Can't downgrade database from version `n` to `n-1` on Samsung

陌路散爱 提交于 2019-12-05 00:20:27

This is the default implementation of SQLiteOpenHelper.onDowngrade(...):

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

As you see if you call super.onDowngrade(...), as you do, you'll get that exception. You need to implement onDowngrade yourself, without calling super.onDowngrade. It should always be implemented for the sake of completeness, as there's no guarantee when it might be called - it sounds strange that the user has changed to use an older version of the app but there might be a situation like that. Do you know what version of the app the exceptions come from?

Your comment in the @etan answer:

why the onDowngrade was called for absolutely no reason?

There is absolute reason,

public static final int DB_V5_USAGE_TABLE = 5;

public static final int DB_VERSION = DB_V5_USAGE_TABLE;

your DB_VERSION holds 5 and in your constructor, you are passing that value. Obviously argument for version should be greater than the previous version otherwise you will get this message.

As @etan expressed, if you need to downgrade the version you need to properly override the onDowngrade method instead throwing the error again.

You may be knew this, so please try to remember your previous version or try to pass 6 or greater for database version parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!