-
-
Save LeonidIvankin/88ca0478812999fb9cabcf2ac9ba1678 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.leonidivankin.draftsandroid.articles.db | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.room.* | |
import androidx.room.OnConflictStrategy.REPLACE | |
class RoomActivity : AppCompatActivity() { | |
private val colorDao by lazy { | |
Room | |
.databaseBuilder(applicationContext, AppDatabase::class.java, "colors_room") | |
.build() | |
.colorDao() | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
Thread { | |
val list = listOf(Color(1, "green"), Color(2, "red"), Color(3, "blue")) | |
colorDao.insert(list) | |
Log.d("DbExample", "${colorDao.get()}") | |
}.start() | |
} | |
} | |
@Database(entities = [Color::class], version = 1, exportSchema = false) | |
abstract class AppDatabase : RoomDatabase() { | |
abstract fun colorDao(): ColorDao | |
} | |
@Entity(tableName = "colors") | |
data class Color(@PrimaryKey val id: Int, val color: String) | |
@Dao | |
interface ColorDao { | |
@Query("SELECT * FROM colors") fun get(): List<Color> | |
@Insert(onConflict = REPLACE) fun insert(color: List<Color>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment