阅读 461

Room的使用

前言

配置dependencies

implementation "androidx.room:room-runtime:2.2.3"
kapt "androidx.room:room-compiler:2.2.3"
复制代码

@Entity配置实体类

@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
)
复制代码

@Dao

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

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(person: Person)
}
复制代码

@ Database创建数据库

@Database(entities = [Person::class], version = 2, exportSchema = false)
abstract class DataBase : RoomDatabase() {
    abstract fun getPersonDao(): PersonDao
    //数据库变动添加Migration,简白的而说就是版本1到版本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")

            }
        }
    }

}
复制代码

创建数据库单例类

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();
}
复制代码

添加查找数据操作


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)
        }
    }
}

复制代码

升级

  • 创建升级的Migration
 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")

            }
        }
    }
复制代码
  • 添加到创建数据库实例中
 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()
复制代码

降级

和升级原理相同