[英]Why Room remove my database after migration
I want create new table in database.我想在数据库中创建新表。 After migration app removed all items in database.
迁移应用程序删除数据库中的所有项目后。 Why?
为什么? I haven't used
fallbackToDestructiveMigration()
我没有使用
fallbackToDestructiveMigration()
My code:我的代码:
public class DatabaseClient {
private static DatabaseClient mInstance;
private DatabaseApp databaseApp;
private DatabaseClient(Context context) {
appDatabase = Room.databaseBuilder(context, DatabaseApp.class, "DB_NAME").build();
appDatabase = Room.databaseBuilder(context, DatabaseApp.class, "DB_NAME").addMigrations(MIGRATION_1_2).build();
}
public static synchronized DatabaseClient getInstance(Context mCtx) {
if (mInstance == null)
mInstance = new DatabaseClient(mCtx);
return mInstance;
}
public AppDatabase getAppDatabase() {
return appDatabase;
}
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull final SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE `MarkRead` (`Id` INTEGER, `StudentId` TEXT," +
"`StudentName` TEXT, `MarkId` INTEGER,`Mark` TEXT, PRIMARY KEY(`Id`))");
}
};
}
What am I doing wrong?我究竟做错了什么?
This is because your old data has not been moved to the new table.这是因为您的旧数据尚未移动到新表中。
You will have to perform the following steps:您必须执行以下步骤:
CREATE TABLE TEMP (... put your columns here)
Fetch the results from your existing table by writing a "SELECT * " query.通过编写“SELECT *”查询从现有表中获取结果。
Insert the results of the previous query into the Temp table.将上一个查询的结果插入到 Temp 表中。
INSERT INTO TEMP SELECT .... FROM ORIGINAL
DROP TABLE ORIGINAL
ALTER TABLE TEMP RENAME TO ORIGINAL
Now you will have a new table with the same name and the data as well.现在您将拥有一个具有相同名称和数据的新表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.