房间持久性库版本2.2.0-alpha01添加了使用预打包数据库的功能。
https://developer.android.com/jetpack/androidx/releases/room
有人可以举例说明如何初始化会议室数据库生成器吗?
答案 0 :(得分:1)
我正在使用这个:
@Database(entities = [Users::class], version = 1, exportSchema = false)
abstract class AppDataBase : RoomDatabase() {
companion object {
private const val DATABASE_NAME = "you_name"
private const val DATABASE_DIR = "database/you_name.db" // Asset/database/you_name.db
fun getInstance(context: Context): AppDataBase {
return Room
.databaseBuilder(context, AppDataBase::class.java, DATABASE_NAME)
.createFromAsset(DATABASE_DIR)
.build()
}
}
abstract fun getUsers(): UsersDao
}
If you need update DB from Asset!
1. You need level up version Database in settings Room!
2. Add .fallbackToDestructiveMigration() method in getInstance
3. And need level up version in you db file;