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

30,080

Solution 1

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.

Solution 2

Had the same problem and I solved it like this.

@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  db.setVersion(oldVersion);
}

Solution 3

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).

Solution 4

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

enter image description here

Share:
30,080
Elad Benda2
Author by

Elad Benda2

Updated on July 20, 2020

Comments

  • Elad Benda2
    Elad Benda2 almost 4 years

    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't release the app and it's for my own use

    I have decided to change the version to 1 again.

    I did fresh install and everything worked fine.

    But then running for the second time throws this error:

    06-05 10:03:35.683: E/AndroidRuntime(9010): android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
    06-05 10:03:35.683: E/AndroidRuntime(9010):     at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:361)
    

    Why is that, after all I did fresh install and the DB should have been removed as well. No?

    How can i change the version to 1 again?

  • Muhammad Babar
    Muhammad Babar about 9 years
    What are the possible reasons for version downgrade?
  • Nasz Njoka Sr.
    Nasz Njoka Sr. about 8 years
    what a question Muhammad Babar...! Well someone want's to drop some conflicting columns from the current schema, someone wants to remove unused columns, some one wants to re define the schema with a couple of new columns and old ones being dropped and decides to drop the whole thing then upgrade?
  • The incredible Jan
    The incredible Jan over 5 years
    @Nasz Njoka Sr. That's nonsense. If you haven't released your app (since the last increase of db version) you can remove all changes. Doesn't matter that you lose some test data. If you released your app you must not downgrade db version. There's never a valid reason to do so. If you want to change somethin on your db upgrade is the way to do it.
  • Samuel T. Chou
    Samuel T. Chou almost 4 years
    @TheincredibleJan well, in my case, my reason to downgrade IS upgrading my db and making an new version of testing app. I was just trying to check how's the whole upgrade going, and to check it, I have to downgrade first -- then upgrade again. (I've tried clean up data and reinstall, but Room still throws exception, don't know why)
  • The incredible Jan
    The incredible Jan over 3 years
    @ Samuel T. Chou You deleted the wrong data (maybe from release build instead of debug). If you test with emulator try the Device File Explorer and delete the files in /data/data/YourAppPath/databases.