android - 在 SQLiteOpenHelper.onDowngrade() 中删除数据库文件的良好模式

标签 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 的包私有(private)部分(例如 .lock() )如果不复制 SQLiteDatabase 就无法替换太(这可能会导致复制整个 sqlite 堆栈)。

有什么好的方法可以做到这一点,还是我必须去 DROP TABLES方式例如像描述的那样here

最佳答案

我已经找到了一种通过扩展 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);
        }
    }
}

关于android - 在 SQLiteOpenHelper.onDowngrade() 中删除数据库文件的良好模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9787712/

相关文章:

android - FileNotFoundException:解码流时没有内容提供者

PHP从URL获取图像到字节数组并反向

java - 在 TextView Switcher 或替代方案中滚动

android - Github 3.0.11 没有git.exe - 如何与Android-Studio 集成

android - 用它的主题、高度和宽度来膨胀布局

java - 可以解析内容uri

android - 如何在库模块中的代码中获取主要的applicationId?

android - 从内部存储创建和共享文件

android - 在 Gradle 中使用构建类型在一台设备上运行使用 ContentProvider 的相同应用程序

android - 何时使用内容提供者