我有一个基于SQLiteOpenHelper
的现有数据库,它有几个版本和代码来升级它,并且工作正常。但是如果用户安装了较旧版本的应用程序(需要较低的数据库版本),它将会崩溃 - 使用它的ContentProvider
无法访问数据库。我想防止它崩溃,但我不想实际降级数据库 - 添加代码来做这件事会很痛苦。删除所有表肯定会起作用,但从一个新文件开始是更清洁,更不容易出错。
这是关于数据库助手的样子 - 没什么特别的
public class MyDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "my.db";
public MyDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
onUpgrade(db, 0, DATABASE_VERSION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion < 1) db.execSQL("CREATE TABLE A...");
if (newVersion < 2) db.execSQL("CREATE TABLE B...");
if (newVersion < 3) db.execSQL("CREATE TABLE C...");
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// I'd like to delete the database here
// the only problem is that I can't from here
// since this is called in the middle of getWritableDatabase()
// and SQLiteDatabase has no .recreate() method.
}
}
我可能采取的方法是:
ContentProvider
中的异常,删除文件并请求再次打开数据库。 - 我不喜欢这样,因为它不是提供者的责任。SQLiteOpenHelper
而不是调用onDowngrade
- 问题是它使用SQLiteDatabase
的包私有部分(例如.lock()
)如果不复制SQLiteDatabase
我就无法取代(这可能会导致重复整个sqlite堆栈)。是否有任何好方法可以做到这一点,或者我必须采用DROP TABLES
方式,例如如描述here?
答案 0 :(得分:9)
通过扩展SQLiteOpenHelper,我找到了一种很好的方法,我需要在MyDbHelper
中做的就是扩展这个类。
public abstract class DeletingSQLiteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = DeletingSQLiteOpenHelper.class.getSimpleName();
private final File mDatabaseFile;
public DeletingSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
mDatabaseFile = context.getDatabasePath(name);
}
public DeletingSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
mDatabaseFile = context.getDatabasePath(name);
}
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
try {
return super.getWritableDatabase();
} catch (SQLiteDowngradeFailedException e) {
// that's our notification
}
// try to delete the file
mDatabaseFile.delete()
// now return a freshly created database
return super.getWritableDatabase();
}
@Override
public final void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// throwing a custom Exception to catch it in getWritableDatabase
throw new SQLiteDowngradeFailedException();
}
// that's the exception
static class SQLiteDowngradeFailedException extends SQLiteException {
public SQLiteDowngradeFailedException() {}
public SQLiteDowngradeFailedException(String error) {
super(error);
}
}
}