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.
// Using this fallback is almost certainly a bad idea
Database database = Room.databaseBuilder(context, Database.class, DATABASE_NAME)
.fallbackToDestructiveMigration()
.build();
@Database(entities = {CallRecording.class}, version = DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {
public abstract RecordingDAO recordingDAO();
// static final Migration MIGRATION_1_2 = new Migration(1, 2) {
// @Override
// public void migrate(SupportSQLiteDatabase database) {
// // Since we didn't alter the table, there's nothing else to do here.
// }
// };
}
private void checkIdentity(SupportSQLiteDatabase db) {
String identityHash = null;
if (hasRoomMasterTable(db)) {
Cursor cursor = db.query(new SimpleSQLiteQuery(RoomMasterTable.READ_QUERY));
//noinspection TryFinallyCanBeTryWithResources
try {
if (cursor.moveToFirst()) {
identityHash = cursor.getString(0);
}
} finally {
cursor.close();
}
}
if (!mIdentityHash.equals(identityHash) && !mLegacyHash.equals(identityHash)) {
throw new 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.");
}
}
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
@Database(entities = arrayOf(Product::class, SatisNok::class), version = 6)
abstract class AppDatabase : RoomDatabase() {
abstract fun productDao(): ProductDao
abstract fun satisDao(): SatisNokDao
}
去活动或者你称之为 db的地方
添加了迁移的方法,这里我把版本从5改成了6
val MIGRATION_1_2: Migration = object : Migration(5,6) {
override fun migrate(database: SupportSQLiteDatabase) {
// Since we didn't alter the table, there's nothing else to do here.
}
}
现在将迁移添加到数据库 建筑工人,作为 .addMigrations(MIGRATION_1_2)
val db = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java, "supervisor"
).fallbackToDestructiveMigration()
.allowMainThreadQueries()
.addMigrations(MIGRATION_1_2)
.build()