思路就是:把旧表改为临时表,建立新表,临时表数据写入新表,删除临时表,不要忘了添加默认字段,废话不多说,直接上代码逻辑
public DaoHelper(Context context,String userId) { DaoMaster.OpenHelper helper = new MigrationHelper(context, "名", null); db = helper.getEncryptedWritableDb("password自己定义"); daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); }
public final class MigrationHelper extends DaoMaster.OpenHelper{
public MigrationHelper(Context context, String name) {
super(context, name);
}
public MigrationHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
new UpgradeHelper().migrate(db,
ChatDao.class,
MediaDao.class,...);
}
}
public class UpgradeHelper {
private static final String CONVERSION_CLASS_NOT_FOUND_EXCEPTION = "MIGRATION HELPER - CLASS DOESN'T MATCH WITH THE CURRENT PARAMETERS";
public void migrate(final Database db, final Class<? extends AbstractDao<?, ?>>... daoClasses) {
generateTempTables(db, daoClasses);
DaoMaster.dropAllTables(db, true);
DaoMaster.createAllTables(db, false);
restoreData(db, daoClasses);
}
private void generateTempTables(Database db, Class<? extends Ab