Skip to content

Moster 06 manage sq lite databases with room#6

Merged
AnelCC merged 4 commits intomasterfrom
Moster_06_ManageSQLiteDatabasesWithRoom
May 1, 2020
Merged

Moster 06 manage sq lite databases with room#6
AnelCC merged 4 commits intomasterfrom
Moster_06_ManageSQLiteDatabasesWithRoom

Conversation

@AnelCC
Copy link
Copy Markdown
Owner

@AnelCC AnelCC commented May 1, 2020

Moster 06 manage sq lite databases with room

1. Define a database table with Room

We using app.cacheDir for get the cache's address from the device.

def room_version = "2.2.5" 
implementation "androidx.room:room-runtime:$room_version"
//Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// For Kotlin use kapt instead of annotationProcessor
annotationProcessor "androidx.room:room-compiler:$room_version"

We add anotation

@Entity(tableName = "monsters")
data class Monster (
    @PrimaryKey(autoGenerate = true)
    val monsterId: Int,

2. Define SQL operations in a DAO interface

We create the Dao class. The purpose of a DAO interface is to define database operations, queries, insert, update and delete operations and so on.

@Dao
interface MonsterDao {
    @Query("SELECT * from monsters")
    fun getAll(): List<Monster>

    @Insert
    suspend fun insertMonster(monster: Monster)

    @Insert
    suspend fun insertMonsters(monsters: List<Monster>)

    @Query("DELETE from monsters")
    suspend fun deleteAll()
} 

3. Define an SQLite database with Room

For each entity class, start with the name of the class, then colon, colon class.(If I had more than one entity I'd list them all here)
The version is always a whole number and it always starts with one for the first version of your database, but then, if you change your database structure later you'll upgrade your version, finally I'll add an export schema property and I'll set that to false. And that just means don't generate files to document the database.

@Database(entities = [Monster::class], version = 1, exportSchema = false)

Then I'll assign instance using this expression: room.databasebuilder.

private var INSTANCE: MonsterDatabase? = null

        fun getDatabase(context: Context): MonsterDatabase {
            if (INSTANCE == null) { ...} }

4. Insert and retrieve data with Room

I'll create an instance of my data access object. I'll call it monsterDao and I'll initialize it with this expression. MonsterDatabase.getDatabase. I'll call MonsterDao. And now I have an object I can use in memory to work with that database table.

 private val monsterDao = MonsterDatabase.getDatabase(app).monsterDao()

I'll create a co-routine so that I'm doing all this work in a background thread.
In a background thread and the toast architecture has to be called from a foreground thread. But co-routines let you switch threads on the fly.

   CoroutineScope(Dispatchers.IO).launch { ... }

I'll call postValue and I'll pass in the data value.

    monsterData.postValue(data)

GetData saveinDB

@AnelCC AnelCC merged commit 4cbf913 into master May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant