版本迭代时难免需要对数据库字段进行增加,删除与修改操作。这时便需要对原有的数据进行迁移,以保证不丢失数据。对数据库的迁移我们需要做一下几个步骤:
具体步骤:
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); } }