Day98 of #100DaysOfCode

Day98 of #100DaysOfCode

Unit 5: Introduction to Databases

class Frog : RealmObject {    
var name: String = ""
var age: Int = 0
var species: String? = null
var owner: String? = null
}
val config = RealmConfiguration.Builder(schema = setOf(Frog::class))
.build()
val realm = Realm.open(config)
val tadpoles: RealmQuery<Frog> = realm.query<Frog>("age > $0", 2)
Log.v("Tadpoles: ${tadpoles.count()}")
val numTadpolesNamedJasonFunderburker = tadpoles.query("name == $0", "Jason Funderburker").count()
Log.v("Tadpoles named Jason Funderburker: $numTadpolesNamedJasonFunderburker")
val config = RealmConfiguration.Builder(schema = setOf(Frog::class))
.build()
val realm = Realm.open(config)
// start a write transaction
realm.writeBlocking {
// get a frog from the database to update
val frog: Frog? = query<Frog>()
.query("name == $0 LIMIT(1)",
"Benjamin Franklin")
.first()
.find()
// update the frog's properties
frog?.apply {
name = "George Washington"
species = "American bullfrog"
}
} // when the transaction completes, the frog's name and species
// are updated in the database

That is all for Day98 ✅

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store