gpt4 book ai didi

android - 即使我更改版本号,Room 也无法验证数据完整性

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:56:22 25 4
gpt4 key购买 nike

我正在创建我的第一个 Room SQLite 迁移,我不知道我现在应该做什么,旧数据库中的版本号是 2,我正在将其更改为 3。我得到以下信息当我尝试编译应用程序时控制台出现错误:

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.

我的数据库代码如下所示:

@Database(entities = {Vehicle.class, ShockAbsorver.class, Customer.class, Review.class, BadRatingOption.class}, version = 3)
public abstract class AppDatabase extends RoomDatabase{
private static AppDatabase INSTANCE;

public abstract VehicleDao vehicleDao();
public abstract ShockAbsorverDao absorverDao();
public abstract CustomerDao customerDao();
public abstract ReviewDao reviewDao();
public abstract OptionDao optionDao();

static final Migration MIGRATION_2_3 = new Migration(2,3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {

database.execSQL("PRAGMA foreign_keys=off;");

// REMOVE COLUMN 'original' FROM Vehicle
database.beginTransaction();
database.execSQL("ALTER TABLE vehicle RENAME TO temp_vehicle;");
database.execSQL("DROP INDEX vehicleRemoteId");
database.execSQL("CREATE TABLE vehicle(id TEXT NOT NULL PRIMARY KEY,idSuperk INTEGER,idRemote TEXT,model TEXT,year INTEGER,plate TEXT,km INTEGER, FOREIGN KEY(idRemote) REFERENCES review(vehicleId));");
database.execSQL("CREATE INDEX vehicleRemoteId ON vehicle(idRemote)");
database.execSQL("INSERT INTO vehicle (id,idRemote,model,year,plate,km) SELECT id,idRemote,model,year,plate,km FROM temp_vehicle;");
database.execSQL("DROP TABLE temp_vehicle;");
database.endTransaction();

// ADD CLOUMN 'id' INTO ShockAbsorver
database.execSQL("ALTER TABLE shockAbsorver ADD COLUMN id TEXT");

// CREATE TABLE BadRatingOption
database.execSQL("CREATE TABLE options (id TEXT NOT NULL PRIMARY KEY,absorverId TEXT,option INTEGER);");
database.execSQL("CREATE UNIQUE INDEX id ON options(id)");
database.execSQL("CREATE INDEX absorverId ON options(absorverId)");

//POPULATE BadRatingOption WITH OPTIONS FROM ShockAbsorver
Cursor cursor = database.query("SELECT * FROM shockAbsorver");

ArrayList<String> reviewIds = new ArrayList<>();
ArrayList<Integer> indexes = new ArrayList<>();
ArrayList<Integer> options = new ArrayList<>();

try {
while (cursor.moveToNext()) {
reviewIds.add(cursor.getString(cursor.getColumnIndex("reviewId")));
indexes.add(cursor.getInt(cursor.getColumnIndex("index")));
options.add(cursor.getInt(cursor.getColumnIndex("option")));

}
} catch (Exception e) {
e.printStackTrace();
}finally{
cursor.close();
}


for(int i = 0;i<reviewIds.size();i++){
String absorverId = UUID.randomUUID().toString();
String optionId = UUID.randomUUID().toString();
database.execSQL("UPDATE shockAbsorver SET id='"+absorverId+"' WHERE reviewId = '"+reviewIds.get(i)+"' AND `index` = "+String.valueOf(indexes.get(i))+";");
database.execSQL("INSERT INTO options (id,absorverId,option) VALUES ('"+optionId+"','"+absorverId+"','"+options.get(i)+"');");
}

reviewIds = null;
indexes = null;
options = null;


//REMOVE 'option' FROM ShockAbsorver
database.beginTransaction();
database.execSQL("ALTER TABLE shockAbsorver RENAME TO temp_shockAbsorver;");
database.execSQL("CREATE TABLE shockAbsorver(`index` INTEGER NOT NULL,id TEXT,remoteId TEXT,isGood INTEGER,rating INTEGER,isNew INTEGER NOT NULL,isEdited INTEGER NOT NULL,isOriginal INTEGER,observation TEXT,filename TEXT,vehicle TEXT NOT NULL,reviewId TEXT NOT NULL,remoteReviewId TEXT,PRIMARY KEY(reviewId,`index`), FOREIGN KEY(reviewId) REFERENCES review(id));");
database.execSQL("INSERT INTO shockAbsorver (`index`,remoteId,isGood,rating,isNew,isEdited,isOriginal,observation,filename,vehicle,reviewId,remoteReviewId) SELECT `index`,remoteId,isGood,rating,isNew,isEdited,isOriginal,observation,filename,vehicle,reviewId,remoteReviewId FROM temp_shockAbsorver;");
database.execSQL("DROP TABLE temp_shockAbsorver;");
database.endTransaction();


database.execSQL("PRAGMA foreign_keys=on;");

}
};

public static AppDatabase getAppDatabase(Context context){
if(INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class,
"shock-absorver-database")
.allowMainThreadQueries()
.addMigrations(MIGRATION_2_3)
.build();
}
return INSTANCE;
}

public static void destroyInstance(){
INSTANCE = null;
}
}

当我编写迁移代码时,我收到“迁移没有正确处理”的消息,直到我修复了所有问题,之后我开始收到数据完整性错误。

提前致谢。

最佳答案

对于生产类型:您需要编写从一个数据库迁移到另一个数据库的代码 https://developer.android.com/training/data-storage/room/migrating-db-versions

对于开发调试版本:尝试从手机中卸载应用程序。 (它将删除旧数据库)。而不是简单地构建并再次运行。

关于android - 即使我更改版本号,Room 也无法验证数据完整性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48650579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com