1. 程式人生 > >GreenDao資料庫加密,升級

GreenDao資料庫加密,升級

用到的依賴:

implementation 'org.greenrobot:greendao:3.2.2'
implementation 'net.zetetic:android-database-sqlcipher:3.5.2'
implementation 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.1.0'

一.加密

加密用到 DaoMaster.OpenHelper ,只需要把 DaoMaster 的引數db換成DaoMaster.OpenHelper的 getEncryptedReadableDb(password)即可。

程式碼:

daoMaster = new DaoMaster(helper.getEncryptedReadableDb("password"));

二.資料庫升級

程式碼:

public class MySqlLiteOpenHelper extends DaoMaster.OpenHelper {

    public MDSqlLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        super.onUpgrade(db, oldVersion, 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);
            }
        },
                Test1Dao.class,
                Test2Dao.class);
    }
}