Crash proof database: reset if error occurs

This commit is contained in:
topjohnwu 2017-11-18 05:03:01 +08:00
parent c8fd5da2da
commit e518f4cef8
2 changed files with 61 additions and 50 deletions

View File

@ -38,6 +38,7 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
if (oldVersion < 3) { if (oldVersion < 3) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL( db.execSQL(
@ -48,6 +49,11 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
mm.prefs.edit().remove(Const.Key.ETAG_KEY).apply(); mm.prefs.edit().remove(Const.Key.ETAG_KEY).apply();
oldVersion = 3; oldVersion = 3;
} }
} catch (Exception e) {
e.printStackTrace();
// Reset database
onDowngrade(db, DATABASE_VER, 0);
}
} }
@Override @Override

View File

@ -114,6 +114,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
if (oldVersion == 0) { if (oldVersion == 0) {
createTables(db); createTables(db);
oldVersion = 3; oldVersion = 3;
@ -158,6 +159,10 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
// Hard link our DB globally // Hard link our DB globally
Shell.su_raw("ln " + getDbFile() + " " + GLOBAL_DB); Shell.su_raw("ln " + getDbFile() + " " + GLOBAL_DB);
} }
} catch (Exception e) {
e.printStackTrace();
onDowngrade(db, DATABASE_VER, 0);
}
} }
@Override @Override