Skip to content

Commit

Permalink
解决数据迁移崩溃问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Oct 8, 2021
1 parent b4cd126 commit e5d4adb
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions accountcache/src/main/java/com/hss01248/accountcache/MyDbUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,34 @@ private static void initGreenDao(Application context) {
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
//兼容旧数据迁移情况
new Thread(new Runnable() {
@Override
public void run() {
moveOldData(context);
}
}).start();
moveOldData(context);

}

private static void moveOldData(Application context) {
Context context2 = new MyDBContext(context);
File file = context2.getDatabasePath(AccountCacher.dbName+"testaccount2.db");
try {
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().insertOrReplaceInTx(debugAccounts);
daoSession0.getDebugAccountDao().deleteAll();
db.close();
//删库跑路
file.delete();
}catch (Throwable throwable){
throwable.printStackTrace();
}


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

0 comments on commit e5d4adb

Please sign in to comment.