Can't downgrade database from version 2 to 1 even after fresh install and re-run

后端 未结 10 980
离开以前
离开以前 2020-12-16 01:14

I\'m writing an android app using SQLite DB.

I had few experiments and changed the DB version from 1 to 2.

Then my DB schema became stable and because i didn

相关标签:
10条回答
  • 2020-12-16 01:39

    You can override the onDowngrade() by your own if you want to be able to run your application with a database on the device with a higher version than your code can handle.

    This is the default implementation of onDowngrade():

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

    If you need to override this method please read this question also.

    Without override that method, we cannot decrease the SQLite version after increased and executed. So you need to change back to 2 or greater than 2:

    public DatabaseHelper(Context context) {
       super(context, DB_NAME, null, 2);
    }
    

    If you are trying to update your table, probably need to pass 3 not 2 and each and every time you need to update the table, you need to increase the version(not decrease).

    0 讨论(0)
  • 2020-12-16 01:43

    This exception happens when the database needs to be downgraded, but your SQLiteOpenHelper-derived class does not implement the onDowngrade callback.

    0 讨论(0)
  • 2020-12-16 01:44

    download the sqlite db file from Android DeviceFileExplorer

    [data/data/your package name/databases]

    open the file using (DB Browser for SQLite)

    goto Edit Pragmas tab and downgrade the db version from User version

    finally save the file and upload it to DeviceFileExplorer

    0 讨论(0)
  • 2020-12-16 01:47

    Had the same problem and I solved it like this.

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.setVersion(oldVersion);
    }
    
    0 讨论(0)
  • 2020-12-16 01:49

    How did you perform the fresh install?

    • adb install -r? In that case, the data of you app, including the database(s), won't be touched.
    • adb uninstall and adb install? This would be the right way to perform a clean installation. All data will be removed during the uninstall and your database will be freshly created after the first start after the install.

    But you write that during the first start after the fresh install, it worked. This can only mean that you somewhere still perform a upgrade to version 2.

    0 讨论(0)
  • 2020-12-16 01:50

    This exception is thrown on the following conditions:

    • The device that you're running the code on has a database file of version 2.
    • The code is requesting version 1 of the database (with a param to SQLiteOpenHelper constructor)
    • onDowngrade() is not overridden in your code.

    You say the code worked fine the first time after a fresh install. Make sure there's no other code that would bump up the version number of the same database file to 2 again.

    0 讨论(0)
提交回复
热议问题