无法将数据库从版本"n"降级为"n-1"在三星上

XGo*_*het 12 android sqliteopenhelper

我有一个数据库的应用程序,使用标准的SQLiteOpenHelper创建和打开.

每当我升级数据库版本时,我也会升级应用程序的版本代码,因此数据库无法关闭(数据库版本号总是增加,永不减少).

我通过将android:allowBackup属性设置为false来禁用我的应用程序中的数据库备份.

但是当我在Play商店升级应用程序时,我遇到了很多崩溃

无法将数据库从版本降级nn-1

这些崩溃中有96%发生在运行的三星设备上.任何人都知道为什么会出现这个问题,更重要的是如何防止这种崩溃?

我知道我可以覆盖onDowngrade以防止崩溃但我实际上不明白为什么onDowngrade被调用,因为在总是使用数据库的最后版本的应用程序上调用了崩溃.

编辑:添加代码示例,FWIW

我的OpenHelper:

public class MyDBHelper extends SQLiteOpenHelper {

    private static final String LOG_TAG = MyDBHelper.class.getName();

    public static final String DB_NAME = "my_db";
    public static final int DB_V1 = 1;
    public static final int DB_V2_UNIQUE_IDS = 2;
    public static final int DB_V3_METADATAS = 3;
    public static final int DB_V4_CORRUPTED_IDS = 4;
    public static final int DB_V5_USAGE_TABLE = 5;

    public static final int DB_VERSION = DB_V5_USAGE_TABLE;

    public MyDBHelper(final Context context, IExceptionLogger logger) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(final SQLiteDatabase db) {
        Debug.log_d(DebugConfig.DEFAULT, LOG_TAG, "onCreate()");
        db.execSQL(createMyTable());
    }

    @Override
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
        Debug.log_d(DebugConfig.DEFAULT, LOG_TAG, "onUpgrade(): oldVersion = " + oldVersion + " : newVersion = " + newVersion);

        if (oldVersion < 2) {
            Debug.log_d(DebugConfig.DEFAULT, LOG_TAG, "onUpgrade(): upgrading version 1 table to version 2");
            db.execSQL(upgradeTable_v1_to_v2());
        }

        if (oldVersion < 3) {
            Debug.log_d(DebugConfig.DEFAULT, LOG_TAG, "onUpgrade(): upgrading version 2 Entry table to version 3");
            db.execSQL(upgradeTable_v2_to_v3());
        }
    }

    @Override
    @TargetApi(Build.VERSION_CODES.FROYO)
    public void onDowngrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
        Debug.log_d(DebugConfig.DEFAULT, LOG_TAG, "onDowngrade(): oldVersion = " + oldVersion + " : newVersion = " + newVersion);
        super.onDowngrade(db, oldVersion, newVersion);
    }
}
Run Code Online (Sandbox Code Playgroud)

以及我如何初始化它:

public class DatabaseController {

    private MyDBHelper mDBHelper;

    public void initialize(final Context context) {

       mDBHelper = new MyDBHelper(context);

    }
}
Run Code Online (Sandbox Code Playgroud)

eta*_*tan 8

这是默认的实现SQLiteOpenHelper.onDowngrade(...):

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    throw new SQLiteException("Can't downgrade database from version " +
            oldVersion + " to " + newVersion);
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的super.onDowngrade(...)那样,如你所做的那样,你会得到那个例外.你需要onDowngrade自己实现,而不是打电话super.onDowngrade.它应该总是为了完整性而实现,因为无法保证它何时可以被调用 - 用户已经改变使用旧版本的应用程序但是可能存在类似情况,这听起来很奇怪.你知道例外来自哪个版本的应用程序?