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

时间:2012-03-20 13:27:44

标签: 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.
    }
}

我可能采取的方法是:

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

是否有任何好方法可以做到这一点,或者我必须采用DROP TABLES方式,例如如描述here

1 个答案:

答案 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);
        }
    }
}