如何在使用Room Persistence Library时升级数据库时获得回调?我提供了Migration类来在升级Database时在表中添加/删除一些列。
答案 0 :(得分:1)
您需要提供下面提到的Room Database回调。
return Room.databaseBuilder(application, WalletDatabase.class, "wallet.db")
.addMigrations(MIGRATION_1_3)
.addMigrations(MIGRATION_2_3)
.addCallback(callback)
.build();
如下所述声明回调。
public static RoomDatabase.Callback callback = new RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
//DO AS NEEDED
}
@Override
public void onOpen(@NonNull SupportSQLiteDatabase db) {
super.onOpen(db);
//DO AS NEEDED
}
};