preface

  • Demo address: github.com/AfterChrist…
  • If useful to you, please like favorites, thank you.

Configuration dependencies

implementation "Androidx. Room: a room - the runtime: 2.2.3"
kapt "Androidx. Room: a room - the compiler: 2.2.3"
Copy the code

@entity Configures the Entity class

@Entity
data class Person(
    @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val cache_id: Long = 0.@ColumnInfo(name = "title") val title: String,
    @ColumnInfo(name = "type") val type: Int.@ColumnInfo(name = "age" ) val age: Int
)
Copy the code

@Dao

@Dao
interface PersonDao {
    @Query("Select * from person")
    fun getAll(a):List<Person>;

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(person: Person)
}
Copy the code

@database Creates a Database

@Database(entities = [Person::class], version = 2, exportSchema = false)
abstract class DataBase : RoomDatabase() {
    abstract fun getPersonDao(a): PersonDao
    // Database changes add Migration, which is short for version 1 to version 2

    companion object{
        val MIGRATION_1_2: Migration = object : Migration(1.2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                Log.e("size=="."update--------------------")
                database.execSQL("ALTER TABLE Person ADD age INTEGER NOT NULL DEFAULT 0")}}}}Copy the code

Create a database singleton class

object DBInstance {
    private const val DB_NAME = "wan.db"
    private val instance: DataBase by lazy {
        Room.databaseBuilder(WanApplication.instance, DataBase::class.java.DB_NAME)
            .addCallback(object : RoomDatabase.Callback() {
                override fun onCreate(db: SupportSQLiteDatabase) {
                    super.onCreate(db)
                    Log.e("db"."create")}override fun onOpen(db: SupportSQLiteDatabase) {
                    super.onOpen(db)
                    Log.e("db"."open")
                }
            })
            .allowMainThreadQueries()
            .addMigrations(DataBase.MIGRATION_1_2)
            .build()
    }
    val personDao = instance.getPersonDao();
}
Copy the code

Add a find data operation


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bt_click.setOnClickListener {
            val person = Person(100L, "ceshi".200.20)
            DBInstance.personDao.insert(person)
        }
        bt_get.setOnClickListener {
            val person = DBInstance.personDao.getAll().get(0)
            Log.e("size==", person.title)
        }
    }
}

Copy the code

upgrade

  • Create the Migration for the upgrade
 companion object{
        val MIGRATION_1_2: Migration = object : Migration(1.2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                Log.e("size=="."update--------------------")
                database.execSQL("ALTER TABLE Person ADD age INTEGER NOT NULL DEFAULT 0")}}}Copy the code
  • Add to create database instance
 Room.databaseBuilder(WanApplication.instance, DataBase::class.java.DB_NAME)
            .addCallback(object : RoomDatabase.Callback() {
                override fun onCreate(db: SupportSQLiteDatabase) {
                    super.onCreate(db)
                    Log.e("db"."create")}override fun onOpen(db: SupportSQLiteDatabase) {
                    super.onOpen(db)
                    Log.e("db"."open")
                }
            })
            .allowMainThreadQueries()
            .addMigrations(DataBase.MIGRATION_1_2)
            .build()
Copy the code

demotion

It works the same way as upgrade