创建Migration
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
//在表device_init中添加TEXT字段mimc
database.execSQL("ALTER TABLE device_init"
+ " ADD COLUMN mimc TEXT");
//在表device_init中添加TEXT字段appId
database.execSQL("ALTER TABLE device_init"
+ " ADD COLUMN appId TEXT");
//创建新表connect_prod并添加对应的字段
//PRIMARY KEY(id)将id设置为主键,NOT NULL设置对应的键不能为空
database.execSQL("CREATE TABLE connect_prod(icon TEXT,local_file_path TEXT,action_res TEXT,url TEXT,idx TEXT,thumb_image_url TEXT," +
"title TEXT,name TEXT,online TEXT,down TEXT,type TEXT,down_load_complete INTEGER NOT NULL,id TEXT NOT NULL,res TEXT,PRIMARY KEY(id))");
}
};
在数据库中添加Migration
{
Room.databaseBuilder(context.getApplicationContext(),
HuLianDatabase.class, "RoomUse.db")
.addMigrations(MIGRATION_1_2)
.build();
}
网友评论