1. 程式人生 > >Android資料庫升級,資料遷移

Android資料庫升級,資料遷移

版本迭代時難免需要對資料庫欄位進行增加,刪除與修改操作。這時便需要對原有的資料進行遷移,以保證不丟失資料。對資料庫的遷移我們需要做一下幾個步驟:

  • ① 建立臨時表,將原來的資料複製到臨時表中。
  • ② 刪除原表,原表資料已經備份至臨時表中,不再需要,進行刪除。
  • ③ 建立新表,呼叫建立新表的方法,建立所有的新表。
  • ④ 恢復原表的資料,將臨時表中的資料恢復至新表中。
  • ⑤ 刪除臨時表,完成所有的步驟。

具體步驟:

1.引入GreenDaoUpgradeHelper

compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.2'

2.在新建DaoManager類

public class DaoManager {

private DaoMaster mDaoMaster;

private DaoSession mDaoSession;

private static volatile DaoManager mInstance = null;

public static final String DATABASE_NAME = "greendao";

private DaoManager(Context context) {

MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, DATABASE_NAME, null);

mDaoMaster = new DaoMaster(

helper.getWritableDatabase());

/* AppOpenHelper helper = new AppOpenHelper(CCApplication.getInstance().getApplicationContext(), DATABASE_NAME);

Database db = helper.getWritableDb();

mDaoMaster = new DaoMaster(db);*/

mDaoSession = mDaoMaster.newSession();

}

public static DaoManager getInstance(Context context)

{

if (mInstance == null) {

synchronized (DaoManager.class) {

if (mInstance == null) {

mInstance = new DaoManager(context);

}

}

}

return mInstance;

}

public DaoMaster getMaster() {

return mDaoMaster;

}

public DaoSession getSession() {

return mDaoSession;

}

public DaoSession getNewSession() {

mDaoSession = mDaoMaster.newSession();

return mDaoSession;

}

/*private static class AppOpenHelper extends DaoMaster.OpenHelper {

public AppOpenHelper(Context context, String name) {

super(context, name);

}

public AppOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {

super(context, name, factory);

}

@Override

public void onUpgrade(Database db, int oldVersion, int newVersion) {

DaoSession daoSession = new DaoMaster(db).newSession();

// TODO: do something before recreate tables

DaoMaster.dropAllTables(db, true);

onCreate(db);

}

@Override

public void onCreate(Database db) {

Log.i("greenDAO", "Creating tables for schema version " + DaoMaster.SCHEMA_VERSION);

DaoMaster.createAllTables(db, true);

}

}*/

public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {

public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {

super(context, name, factory);

}

@Override

public void onUpgrade(Database db, int oldVersion, int newVersion) {

MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {

@Override

public void onCreateAllTables(Database db, boolean ifNotExists) {

DaoMaster.createAllTables(db, ifNotExists);

}

@Override

public void onDropAllTables(Database db, boolean ifExists) {

DaoMaster.dropAllTables(db, ifExists);

}

},DBCurrentUserDao.class, DBCurrentUserSettingDao.class, DBTextChatMessageDao.class,RelationUserDao.class);

}

}

}


主要程式碼:

public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
}
    @Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
        MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {

            @Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
                DaoMaster.createAllTables(db, ifNotExists);
}

            @Override
public void onDropAllTables(Database db, boolean ifExists) {
                DaoMaster.dropAllTables(db, ifExists);
}
        },DBCurrentUserDao.class, DBCurrentUserSettingDao.class, DBTextChatMessageDao.class,RelationUserDao.class);
}
}