如果仅添加新 table,则反应性 android 数据库迁移
Reactive android database migration if only new table is added
我为数据库添加了新的库,我在使用 ReActive android 数据库的 运行 程序时遇到此错误。
无法创建应用程序 com.reactiveandroid.sample.App:java.lang.IllegalArgumentException:SQL 文件 assets/from_2_to_3。sql 找到注释。
@Database(name = "AppDatabase", version = 3)
public class AppDatabase {
static final Migration MIGRATION_1_2=new Migration(1,2) {
@Override
public void migrate(SQLiteDatabase database) {
AssetsSqlMigration.executeSqlScript(database,"assets/from_1_to_2.sql");
}
};
static final Migration MIGRATION_2_3=new Migration(2,3) {
@Override
public void migrate(SQLiteDatabase database) {
AssetsSqlMigration.executeSqlScript(database,"assets/from_2_to_3.sql");
}
};
}
public class 应用程序扩展了应用程序{
@Override
public void onCreate() {
super.onCreate();
DatabaseConfig appDatabaseConfig = new DatabaseConfig.Builder(AppDatabase.class)
.addModelClasses(Note.class, Folder.class, NoteFolderRelation.class)
.addMigrations(AppDatabase.MIGRATION_1_2 ,MIGRATION_2_3)
.disableMigrationsChecking()
.build();
ReActiveAndroid.init(new ReActiveConfig.Builder(this)
.addDatabaseConfigs(appDatabaseConfig)
.build());
}
}
ALTER TABLE Note ADD COLUMN nameid INTEGER;
您需要在 migrate()
中编写 DDL 查询,如下所示。
static final Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(SupportSQLiteDatabase database) {
// if you need to create new table
database.execSQL("ALTER TABLE your_table "
+ " ADD COLUMN column_name DATATYPE");
}
};
我为数据库添加了新的库,我在使用 ReActive android 数据库的 运行 程序时遇到此错误。
无法创建应用程序 com.reactiveandroid.sample.App:java.lang.IllegalArgumentException:SQL 文件 assets/from_2_to_3。sql 找到注释。
@Database(name = "AppDatabase", version = 3)
public class AppDatabase {
static final Migration MIGRATION_1_2=new Migration(1,2) {
@Override
public void migrate(SQLiteDatabase database) {
AssetsSqlMigration.executeSqlScript(database,"assets/from_1_to_2.sql");
}
};
static final Migration MIGRATION_2_3=new Migration(2,3) {
@Override
public void migrate(SQLiteDatabase database) {
AssetsSqlMigration.executeSqlScript(database,"assets/from_2_to_3.sql");
}
};
}
public class 应用程序扩展了应用程序{
@Override
public void onCreate() {
super.onCreate();
DatabaseConfig appDatabaseConfig = new DatabaseConfig.Builder(AppDatabase.class)
.addModelClasses(Note.class, Folder.class, NoteFolderRelation.class)
.addMigrations(AppDatabase.MIGRATION_1_2 ,MIGRATION_2_3)
.disableMigrationsChecking()
.build();
ReActiveAndroid.init(new ReActiveConfig.Builder(this)
.addDatabaseConfigs(appDatabaseConfig)
.build());
}
}
ALTER TABLE Note ADD COLUMN nameid INTEGER;
您需要在 migrate()
中编写 DDL 查询,如下所示。
static final Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(SupportSQLiteDatabase database) {
// if you need to create new table
database.execSQL("ALTER TABLE your_table "
+ " ADD COLUMN column_name DATATYPE");
}
};