GreenDao升级时不要重建所有表

GreenDao的配置写在build.gradle,如下:

greendao {
    schemaVersion 1    //<--数据库的版本,用于升级时候进行更改
    daoPackage 'com.zhuyefeng.green.gen'     //存放自动生成代码的包
    targetGenDir 'src/main/java'                  //...
}

GreenDao会自动生成DaoMaster、DaoSession两个类。 DaoMaster中又包含OpenHelper、DevOpenHelper两个内部类。

简单使用(强烈建议不要这样用):

public class GreenDaoHelper {

    private static SQLiteDatabase database;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    //单例模式
    private static GreenDaoHelper mInstance;

    private GreenDaoHelper() {
        //初始化建议放在Application中进行
        if (mInstance == null) {
            //创建数据库"info.db"
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApplication.getContext(), "info.db", null);
            //获取可写数据库
            database = devOpenHelper.getWritableDatabase();
            //获取数据库对象
            daoMaster = new DaoMaster(database);
            //获取Dao对象管理者
            daoSession = daoMaster.newSession();
        }
    }

    public static GreenDaoHelper getInstance() {
        if (mInstance == null) {
            //保证异步处理安全操作
            synchronized (GreenDaoHelper.class) {
                if (mInstance == null) {
                    mInstance = new GreenDaoHelper();
                }
            }
        }
        return mInstance;
    }

    public DaoMaster getDaoMaster() {
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

}

如果直接这样使用,那么每次升级版本号,GreenDao都会重建数据库中所有表。原因可以看一下DevOpenHelper类中的onUpgrade方法,里面的逻辑就是重建所有表。

如果想要每次升级按需更新,建议这样写:

public class GreenDaoHelper {

    private static SQLiteDatabase database;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    //单例模式
    private static GreenDaoHelper mInstance;

    private GreenDaoHelper() {
        //初始化建议放在Application中进行
        if (mInstance == null) {
            //创建数据库"info.db"
            DaoMaster.OpenHelper openHelper = new DaoMaster.OpenHelper(MyApplication.getContext(), "info.db"){
                @Override
                public void onUpgrade(Database db, int oldVersion, int newVersion) {
                    if(oldVersion < 2) {
                            db.beginTransaction();
                            db.execSQL("...");
                            db.setTransactionSuccessful();
                            db.endTransaction();
                    }
                    if(oldVersion < 3) {
                            db.beginTransaction();
                            db.execSQL("...");
                            db.setTransactionSuccessful();
                            db.endTransaction();
                    }
                }
            };

            //获取可写数据库
            database = openHelper.getWritableDatabase();
            //获取数据库对象
            daoMaster = new DaoMaster(database);
            //获取Dao对象管理者
            daoSession = daoMaster.newSession();
        }
    }

    public static GreenDaoHelper getInstance() {
        if (mInstance == null) {
            //保证异步处理安全操作
            synchronized (GreenDaoHelper.class) {
                if (mInstance == null) {
                    mInstance = new GreenDaoHelper();
                }
            }
        }
        return mInstance;
    }

    public DaoMaster getDaoMaster() {
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

}

判断旧版本是否小于某个版本,如果小于这个版本,则做这个版本需要做的升级操作。

你可能感兴趣的:(【Android进阶】,GreenDao,SQLite)