Room cannot verify the data integrity Ask Question

Room cannot verify the data integrity Ask Question

I am getting this error while running a program with Room Database

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.

It seems we need to update Database version, but from where can we do that in Room?

ベストアンサー1

When you first come across this message, you will most likely be working against an unreleased version of the database. If that is the case, most likely you should not increment the database versionアプリのデータをクリアするだけで例外を回避できます。アプリがライブの場合は、データベースのバージョンを上げて適切な移行を行う必要がある可能性があります。

データベースを増分しない場合は(推奨):

Android 設定からアプリケーションのアプリデータを消去する必要があります。または、以前のバージョンのアプリをアンインストールしてから新しいバージョンをインストールして例外を回避できる場合があります。この後者の方法は、特定の条件 (バックアップの許可が有効になっている場合など) では機能しません。

アプリケーション データを消去すると必ず機能するので、私は毎回その方法を採用しています。

データベースのバージョンを増やす場合:

データベーススキーマの変更を考慮してデータベース移行コードを記述する必要があります。ここ移行に関する情報については、こちらをご覧ください。

データベース移行コードを書く代わりに、fallbackToDestructiveMigrationRoom データベース ビルダーを呼び出すこともできます。この変更は実際のユーザーに公開すべきではないため、これはおそらく良い考えではありません。この呼び出しを削除し忘れ、データベースをアップグレードし忘れると、ユーザーのデータが失われることになります。

// Using this fallback is almost certainly a bad idea
Database database = Room.databaseBuilder(context, Database.class, DATABASE_NAME)
        .fallbackToDestructiveMigration()
        .build();

繰り返しになりますが、以前のデータベーススキーマがライブでない場合は、データベースバージョンをインクリメントしたり、破壊的な移行にフォールバックしたりする必要はありません。野生で

おすすめ記事