greenDAO数据库之升级

传送门-->索引

一、GreenDaoUpgradeHelper开源库 传送门-->GreenDaoUpgradeHelper

  • 开源库自我介绍 :GreenDaoUpgradeHelper is a database upgrade helper for greenDao.Use GreenDaoUpgradeHelper can easy solve database upgrade that's just need one line code.(~. .^)我……算了。
  • 如何使用:

1.Add it in your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

2.Add the dependency(greendao 3.0 above)

    dependencies {
            compile 'org.greenrobot:greendao:3.2.0'
            compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.3'
    }

or (greendao 3.0 below)

    dependencies {
            compile 'de.greenrobot:greendao:2.0.0'
            compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.0.1'
    }

3.Add a new class that extends DaoMaster.OpenHelper,add a constructor,implement onUpgrade method and called migratemethod,arguments is all of generated Dao class:

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);
            }
        },TestDataDao.class, TestData2Dao.class, TestData3Dao.class);

like this:

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);
                    }
                },TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
    }
}

4.Finally,init like this:

//MigrationHelper.DEBUG = true;  //if you want see the log info,default is false
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "test.db",
                null);
        daoMaster = new DaoMaster(helper.getWritableDatabase());

5.ProGuard rule

-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
    public static void dropTable(org.greenrobot.greendao.database.Database, boolean);
    public static void createTable(org.greenrobot.greendao.database.Database, boolean);
}

二、集成到自己的项目中
基于上一篇封装的DaoManager,在该类中添加静态内部类MySQLiteOpenHelper

注意:bean类的变量类型最好用引用类型的,如果你在表中新加一个int变量字段,那数据库更新后数据库会被清空。

/**
 * Creator: Jowney  (~._.~)
 * Date: 2018/5/13/20:18
 * Description:
 */

public class DaoManager {
    private static final String DB_NAME = "jDatabase.db";//数据库名称
    private static DaoManager mDaoManager;
    private DaoManager.MySQLiteOpenHelper mHelper;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private Context context;
    private boolean encrypt = false;//数据库加密,默认不加密
    private String password =null;
    public DaoManager(Context context) {
        this.context = context;
    }

    /**
     * 使用单例模式获得操作数据库的对象
     *
     * @return
     */
    public static DaoManager getInstance(Context context) {
        if (mDaoManager == null) {
            synchronized (DaoManager.class) {
                if (mDaoManager == null) {
                    mDaoManager = new DaoManager(context);
                }
            }
        }
        return mDaoManager;
    }

    /**
     * 获取DaoSession
     *
     * @return
     */
    public synchronized DaoSession getDaoSession() {
        if (null == mDaoSession) {
            mDaoSession = getDaoMaster().newSession();
        }
        return mDaoSession;
    }

    /**
     * 设置debug模式开启或关闭,默认关闭
     *
     * @param flag
     */
    public  void setDebug(boolean flag) {
        QueryBuilder.LOG_SQL = flag;
        QueryBuilder.LOG_VALUES = flag;
    }

    /**
     *将标志位设为true
     */
    public void encryptDatabase(String password) {
        this.encrypt = true;
        this.password = password;
    }

    /**
     * 关闭数据库
     */
    public synchronized void closeDataBase() {
        closeHelper();
        closeDaoSession();
    }

    /**
     * 判断数据库是否存在,如果不存在则创建
     *
     * @return
     */
    private DaoMaster getDaoMaster() {
        if (null == mDaoMaster) {
             mHelper = new DaoMaster.MySQLiteOpenHelper(context, DB_NAME, null);   
                mDaoMaster = new DaoMaster(mHelper.getWritableDb());          
        }
        return mDaoMaster;
    }

    private void closeDaoSession() {
        if (null != mDaoSession) {
            mDaoSession.clear();
            mDaoSession = null;
        }
    }

    private void closeHelper() {
        if (mHelper != null) {
            mHelper.close();
            mHelper = null;
        }
    }

    public static 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);
                }
            }, StudentDao.class);
        }
    } 
}

你可能感兴趣的:(greenDAO数据库之升级)