facebook twitter hatena line email

Android/kotlin/db/room

提供: 初心者エンジニアの簡易メモ
2020年2月18日 (火) 17:09時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索

Roomとは

SQLiteのマッパーライブラリ

準備

app/build.gradle

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
dependencies {
    implementation 'android.arch.persistence.room:runtime:1.0.0'
    annotationProcessor  'android.arch.persistence.room:compiler:1.0.0'
    kapt 'android.arch.persistence.room:compiler:1.0.0'
    
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"
}

サンプル

UserRoomDatabase.kt


import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = arrayOf(UserEntity::class), version = 1)
public abstract class UserRoomDatabase: RoomDatabase() {
    abstract fun userDao(): UserDao
    companion object {
        @Volatile
        private var INSTANCE: UserRoomDatabase? = null
        fun getDatabase(context: Context): UserRoomDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    UserRoomDatabase::class.java,
                    "users_database"
                ).build()
                INSTANCE = instance
                return instance
            }
        }
    }
}

UserEntity.kt

import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "users")
data class UserEntity constructor(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    val name: String
)

UserDao.kt

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import androidx.room.Delete
import androidx.room.OnConflictStrategy

@Dao
interface UserDao {
    @Query("SELECT * from users")
    fun getAllUserData(): List<UserEntity>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(user: UserEntity)

    @Update
    fun update(user: UserEntity)

    @Delete
    fun delete(user: UserEntity)

    @Delete
    fun deleteUsers(users: List<UserEntity>)
}

db呼び出し

MainActivity.kt

class MainActivity : AppCompatActivity() {
    val scope = CoroutineScope(Dispatchers.Default)
    companion object {
        lateinit var database: UserRoomDatabase
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        database = UserRoomDatabase.getDatabase(applicationContext)
        scope.launch {
            dbTask()
        }
    }
    private suspend fun dbTask() {
        try {
            dbExec()
        } catch (e: Exception) {
            Log.e(localClassName, "onCancelled", e)
        }
    }
    private fun dbExec() {
        val dao = database.userDao()
        dao.insert(UserEntity(0, "rei"))
        dao.insert(UserEntity(1, "taro"))
        dao.insert(UserEntity(2, "jiro"))
        var users: List<UserEntity> = dao.getAll()
        for (user: UserEntity in users) {
            Log.i("db", "id=" + user.id.toString() + " name=" + user.name);
        }
        for (user: UserEntity in users) {
            if (user.id == 2) {
                Log.i("db", "upd id=" + user.id.toString() +" name=" + user.name);
                dao.update(UserEntity(user.id, "siro"))
            } else {
                Log.i("db", "del id=" + user.id.toString() +" name=" + user.name);
                dao.delete(UserEntity(user.id, user.name));
            }
        }
        users = dao.getAll()
        for (user: UserEntity in users) {
            Log.i("db", "del id=" + user.id.toString() +" name=" + user.name);
            dao.delete(UserEntity(user.id, user.name));
        }
    }
}

出力

2020-01-14 16:16:51.109 24379-24416/? I/db: id=1 name=taro
2020-01-14 16:16:51.109 24379-24416/? I/db: id=2 name=jiro
2020-01-14 16:16:51.109 24379-24416/? I/db: id=21 name=rei
2020-01-14 16:16:51.109 24379-24416/? I/db: del id=1 name=taro
2020-01-14 16:16:51.110 24379-24416/? I/db: upd id=2 name=jiro
2020-01-14 16:16:51.111 24379-24416/? I/db: del id=21 name=rei
2020-01-14 16:16:51.114 24379-24416/? I/db: del id=2 name=siro

idが0のinsertはauto_incrementのmax値が入る?

更新処理

上記例では新規UserEntityを作って入れたが、

dao.update(UserEntity(user.id, "siro"))

変更するプロパティをvalからvarに変えて、更新していれることもできる

UserEntity.kt

data class UserEntity constructor(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    var name: String
)
user.name = "siro"
dao.update(user)

参考

https://re-engines.com/2019/10/24/%E3%80%90kotlin%E3%80%91room%E3%81%A7db%E7%AE%A1%E7%90%86%E3%82%92%E3%81%99%E3%82%8B/