Skip to content

Commit

Permalink
使用不加密的数据库,以避免sqlsipher在低版本崩溃问题,以及旧数据迁移
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Oct 8, 2021
1 parent db966ef commit 5ed15bd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
3 changes: 2 additions & 1 deletion accountcache/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ dependencies {
implementation 'org.greenrobot:greendao:3.3.0'
api "com.google.code.gson:gson:2.8.5"
implementation "net.zetetic:android-database-sqlcipher:4.4.3"
implementation "androidx.sqlite:sqlite:2.0.1"
api "androidx.sqlite:sqlite:2.0.1"
api 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1'
}
apply plugin: 'org.greenrobot.greendao'
greendao {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase
SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null);
return result;
}

}
31 changes: 28 additions & 3 deletions accountcache/src/main/java/com/hss01248/accountcache/MyDbUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.hss01248.accountcache.db.DaoMaster;
import com.hss01248.accountcache.db.DaoSession;
import com.hss01248.accountcache.db.DebugAccountDao;

import org.greenrobot.greendao.database.Database;

import java.io.File;
import java.util.List;

public class MyDbUtil {
Expand All @@ -18,11 +18,36 @@ public class MyDbUtil {
* 初始化GreenDao,直接在Application中进行初始化操作
*/
private static void initGreenDao(Application context) {
//指定数据库存储路径
Context context2 = new MyDBContext(context);
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context2, AccountCacher.dbName+"testaccount2.db");
Database db = helper.getEncryptedReadableDb(AccountCacher.dbName+"856yuv98");
//升级自动迁移数据的工具
DaoMaster.OpenHelper helper = new MySQLiteUpgradeOpenHelper(context2, AccountCacher.dbName+"testaccount3.db");
Database db = helper.getWritableDb();
//不再加密.以规避sqlitesipher在6.0以下版本的c层崩溃问题
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
//兼容旧数据迁移情况
moveOldData(context);
}

private static void moveOldData(Application context) {
Context context2 = new MyDBContext(context);
File file = context2.getDatabasePath(AccountCacher.dbName+"testaccount2.db");

if(file == null || !file.exists()){
return;
}
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context2, AccountCacher.dbName+"testaccount2.db");
//数据库加解密
Database db = helper.getEncryptedWritableDb(AccountCacher.dbName+"856yuv98");
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession0 = daoMaster.newSession();
List<DebugAccount> debugAccounts = daoSession0.getDebugAccountDao().loadAll();
daoSession.getDebugAccountDao().insertInTx(debugAccounts);
daoSession0.getDebugAccountDao().deleteAll();
db.close();
//删库跑路
file.delete();
}

private static DaoSession daoSession;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hss01248.accountcache;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.github.yuweiguocn.library.greendao.MigrationHelper;
import com.hss01248.accountcache.db.DaoMaster;
import com.hss01248.accountcache.db.DebugAccountDao;


import org.greenrobot.greendao.database.Database;

public class MySQLiteUpgradeOpenHelper extends DaoMaster.OpenHelper {
public MySQLiteUpgradeOpenHelper(Context context, String name) {
super(context, name);
}

public MySQLiteUpgradeOpenHelper(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);
}
}, DebugAccountDao.class);
}
}

0 comments on commit 5ed15bd

Please sign in to comment.