我正在阅读有关Android架构组件的房间,并想知道Room中是否存在与SQLiteOpenHelper中可用的onUpgrade方法相同的内容 -
@覆盖 public void onUpgrade(最终的SQLiteDatabase数据库,final int oldVersion,final int newVersion){}
谢谢, RS。
答案 0 :(得分:9)
您可以使用https://developer.android.com/training/data-storage/room/migrating-db-versions.html
Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name")
.addMigrations(MIGRATION_1_2,MIGRATION_1_3, MIGRATION_2_3).build();
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE `Fruit` (`id` INTEGER, "
+ "`name` TEXT, PRIMARY KEY(`id`))");
}
};
static final Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE Book "
+ " ADD COLUMN pub_year INTEGER");
}
};
static final Migration MIGRATION_1_3 = new Migration(1, 3) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE Book "
+ " ADD COLUMN pub_year INTEGER");
}
};
答案 1 :(得分:-3)
在资源文件夹中使用以下库和您自己的sqlite文件,因此创建表没有样板代码
编译'com.readystatesoftware.sqliteasset:sqliteassethelper:+'