在SQLiteOpenHelper.onDowngrade()中删除数据库文件的好架构

zap*_*apl 6 android android-contentprovider

我有一个基于的现有数据库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.
    }
}
Run Code Online (Sandbox Code Playgroud)

我可能采取的方法是:

  • 从外部执行:捕获异常ContentProvider,删除文件并请求再次打开数据库. - 我不喜欢这样,因为它不是提供者的责任.
  • SQLiteOpenHelper用我自己的那个删除文件而不是调用的类的副本替换onDowngrade- 问题是它使用了SQLiteDatabase(例如.lock())的包私有部分,我无法替换而不重复SQLiteDatabase(这可能会导致重复整个sqlite堆栈).

有没有什么好方法可以做到这一点,或者我必须采取行动,DROP TABLES例如这里所描述的?

zap*_*apl 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)