【问题标题】:Room Database migration crash房间数据库迁移崩溃
【发布时间】:2021-09-02 09:30:22
【问题描述】:

我有一个数据库迁移:

val MIGRATION_8_9 = object : Migration(8, 9) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE RideEntity RENAME frontVideoPresent TO frontVideoState")
            database.execSQL("ALTER TABLE RideEntity RENAME rearVideoPresent TO rearVideoState")
  }
}

在本地三星手机上测试此迁移时,它运行良好。在 craslytics 的帮助下,我看到了这个崩溃:

Fatal Exception: java.lang.RuntimeException
Exception while computing database live data.

Caused by android.database.sqlite.SQLiteException
near "frontVideoPresent": syntax error (Sqlite code 1 SQLITE_ERROR): , while 
compiling: ALTER TABLE RideEntity RENAME frontVideoPresent TO frontVideoState, (OS 
error - 11:Try again)

这发生在华为 Mate 20 手机上。如何更好地理解这次崩溃?这与操作系统有关吗? 我现在无法删除重命名,因为许多用户更新了该列重命名的应用程序,但使用华为手机的用户可能会遭受此崩溃。 我愿意接受您的建议...

【问题讨论】:

    标签: android android-room database-migration


    【解决方案1】:

    看起来某些设备上的 sqlite 版本不支持列重命名,因为 android 应用程序使用 sqlite 库上的内置版本到 android 操作系统。这就是为什么 sqlite 的版本取决于 android 的 api 级别版本(应用程序运行的位置)。 According to sqlite release notes(第 2 段)在 3.25.0 版本中添加了对列重命名的支持,according to google docsand other answer on stackoverflow)自 android api 级别 30 起支持在 android 上的列重命名。

    要解决 slqlite 库的碎片问题,您可以使用android-requery,它允许在所有 android 版本中使用最新版本的 sqlile(从 API 级别 14 开始)。很容易use this library with room

    【讨论】:

    • 感谢您的回答。我添加了“openHelperFactory(RequerySQLiteOpenHelperFactory())”。但是我如何采用迁移?覆盖 fun migrate(database: SupportSQLiteDatabase) 需要来自 androidx.sqlite.db 的接口 SupportSQLiteDatabase
    • @Dim RequerySQLiteOpenHelperFactory 创建 androidx.sqlite.db. SupportSQLiteDatabase 并且迁移应该可以正常工作 - 开箱即用。
    • @Dim 你有什么问题吗?
    • 所以我只需要添加“.openHelperFactory(RequerySQLiteOpenHelperFactory())”并保留“import androidx.sqlite.db.SupportSQLiteDatabase”?
    • 非常感谢!
    猜你喜欢
    • 2019-02-13
    • 2021-08-13
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2018-11-11
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多