greendao升级数据库GreenDaoUpgradeHelper

1、修改model文件(比如增加一个字段)
2、makeproject 查看dao文件是否修改了
3、greendao {
    schemaVersion 1//数据库版本号 版本号+1
3、implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:2.2.1'
4、初始化数据库改为
//用于升级
//        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, Constants.DB_NAME,
//                null);
//        database = helper.getWritableDatabase();
//        DaoMaster daoMaster = new DaoMaster(database);
//        daoSession = daoMaster.newSession();
4、重新查询数据库表的数据是否被删除了 如果还存在那么是正确的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,GreenDAO支持数据库加密。你可以通过在DaoMaster类中使用SQLCipher来加密数据库。SQLCipher是一个基于SQLite的开源库,可用于加密SQLite数据库。 以下是使用SQLCipher加密GreenDAO数据库的步骤: 1. 在build.gradle文件中添加SQLCipher库的依赖: ``` dependencies { implementation 'net.zetetic:android-database-sqlcipher:4.4.0' } ``` 2. 在DaoMaster类中使用SQLCipher初始化数据库。你需要在使用SQLiteOpenHelper创建数据库之前,调用SQLiteDatabase.loadLibs(context)加载SQLCipher库,并在创建数据库时,指定加密密码。 ```java public class MyDaoMaster extends DaoMaster.OpenHelper { public MyDaoMaster(Context context, String name) { super(context, name); SQLiteDatabase.loadLibs(context); } public MyDaoMaster(Context context, String name, SQLiteDatabase.CursorFactory factory) { super(context, name, factory); SQLiteDatabase.loadLibs(context); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { super.onUpgrade(db, oldVersion, newVersion); // 数据库升级时,需要重新加密数据 db.execSQL("PRAGMA cipher_migrate;"); } @Override public SQLiteDatabase getWritableDatabase() { SQLiteDatabase db = super.getWritableDatabase(SQLCipherUtils.getEncryptionKey()); // 开启数据库加密 db.rawExecSQL("PRAGMA key = '"+SQLCipherUtils.getEncryptionKey()+"';"); db.rawExecSQL("PRAGMA cipher_use_hmac = off;"); db.rawExecSQL("PRAGMA cipher_page_size = 1024;"); db.rawExecSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;"); db.rawExecSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA1;"); return db; } } ``` 3. 在Application类中初始化GreenDAO。你需要使用自定义的MyDaoMaster类,以及指定数据库名称和版本号。 ```java public class MyApplication extends Application { private DaoSession daoSession; @Override public void onCreate() { super.onCreate(); // 初始化GreenDAO DaoMaster.DevOpenHelper helper = new MyDaoMaster(this, "my-db"); Database db = helper.getEncryptedWritableDb(SQLCipherUtils.getEncryptionKey()); daoSession = new DaoMaster(db).newSession(); } public DaoSession getDaoSession() { return daoSession; } } ``` 需要注意的是,加密密码需要妥善保管,不要轻易泄露。你可以通过SQLCipherUtils类来生成加密密码,以及验证密码是否正确。 ```java public class SQLCipherUtils { private static final String KEY = "my-secret-key"; public static String getEncryptionKey() { return KEY; } public static boolean isPasswordCorrect(String password) { return password != null && password.equals(KEY); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值