【发布时间】:2020-04-05 10:36:29
【问题描述】:
在我的应用程序中,我使用的是 Room 数据库。这里要支持离线功能,我需要使用预填充的数据库。
所以,我需要的是,我创建了一个带有值的 Room 数据库,并将该数据库放在项目的 assets 文件夹中,并将数据库导入到我的本地。
现在一切正常。
现在,由于某种原因,我在本地数据库中再添加一列(不是资产文件夹中的外部数据库),但它给了我以下错误。
java.lang.IllegalStateException:迁移没有正确处理
我也添加了迁移。
以下是我的数据库代码。
@Database(
entities = arrayOf(ModelCategories::class, ModelApps::class),
version = 4,
exportSchema = false)
abstract class LauncherDatabase : RoomDatabase() {
abstract fun categoriesDAO(): DAOCategories
abstract fun appsDAO(): DAOApps
companion object {
// Singleton prevents multiple instances of database opening at the
// same time.
@Volatile
private var INSTANCE: LauncherDatabase? = null
/*
When there is change in Databse structure new migration should be there
*/
@JvmField
val MIGRATION_3_2: Migration = object : Migration(3, 4) {
override fun migrate(database: SupportSQLiteDatabase) {
}
}
fun getDatabase(context: Context): LauncherDatabase {
val tempInstance = INSTANCE
if (tempInstance != null) {
return tempInstance
}
synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
LauncherDatabase::class.java,
"launcher_database"
).createFromAsset("databases/launcher_database.db")
.addMigrations(LauncherDatabase.MIGRATION_3_2)
.build()
INSTANCE = instance
return instance
}
}
}}
注意:-外部资产数据库的数据库版本是3。所以,在模型类中添加一列后,我将当前数据库版本增加到4。
提前致谢。
【问题讨论】:
-
您没有在迁移代码中添加任何内容。
-
ohhh....那么我应该在这里写什么呢?我添加了一个额外的列,它在外部数据库中不可用。
-
在我看来,您错过了
ADD COLUMN从 3 到 4 的迁移 -
我明白,但在这里我使用外部数据库作为资产。那么我应该为迁移编写哪些命令?
标签: android android-room android-assets