房间无法验证数据完整性。看起来您已更改架构,但忘记更新版本号


java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

我知道我可以通过增加版本号来解决这个问题。

我正在做一个有很多数据库工作的项目。我可能需要每天至少更改50次数据库结构。所以我每天都要把这个数字增加50或更多。

如果我错过了增加(如果偶然(,我会得到上面的错误。

我试过.fallbackToDestructiveMigration()exportSchema = false网上冲浪的一些建议。

你们中有人能为我提供合适的解决方案吗。

拥有.fallbackToDestinative migration((不是一个很好的选择,因为在将来执行实际迁移时(向世界发布后(,它可能会导致数据丢失。

转到应用程序信息->存储->清除存储。这将清除所有数据并解决您的问题。

只有当您在清单下设置了android:allowBackup="false"时,卸载和重新安装才会有帮助。我建议阅读有关这方面的文档:https://developer.android.com/guide/topics/data/autobackup

注意:不要为了开发/调试构建目的而更改架构版本号

我发现几乎所有关于.fallbackToDestructiveMigration()问题的答案都忽略了一个重要的点。

我们不仅添加";。fallbackToDestinationMigration((&";像这个一样在构建((之前

Room.databaseBuilder(context.applicationContext,AppDatabase::class.java,"app_database")
.fallbackToDestructiveMigration()
.build()

但也需要在代码中升级数据库版本

// before upgrade
@Database(version = 1,entities = [User::class])
// after upgrade
@Database(version = 2,entities = [User::class]) 

只有完成这两个步骤才能解决以下问题。

java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

只需在Emulator或Mobile中卸载并重新安装应用程序。。。这有助于。。。

最新更新