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'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?

Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
  • 1
    It's look like you creating database with version 1 at start and upgrade it somewhere during running. After that you re-run you app and need to downgrade. Post code where you creating your database. – Alexander Mikhaylov Jun 05 '14 at 07:11
  • http://stackoverflow.com/questions/15018025/cant-downgrade-database-from-version-2-to-1 – user Jan 12 '15 at 07:39

10 Answers10

23

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.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • 2
    What are the possible reasons for version downgrade? – Muhammad Babar Feb 18 '15 at 22:09
  • 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? – Nasz Njoka Sr. Feb 19 '16 at 12:52
  • @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. – The incredible Jan Nov 22 '18 at 06:42
  • @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) – Samuel T. Chou Jul 15 '20 at 07:47
  • @ 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. – The incredible Jan Jan 06 '21 at 15:00
6

Had the same problem and I solved it like this.

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

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

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
Blasanka
  • 21,001
  • 12
  • 102
  • 104
2

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

0

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

CL.
  • 173,858
  • 17
  • 217
  • 259
0

You don't show where the database is created, which is where the database version is normally assigned (i.e. SQLiteOpenHelper ctor).

Is there any chance you have the database on external file system? Unless you follow the naming conventions, these files are not deleted when the application is removed.

guycole
  • 788
  • 5
  • 10
0

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.

msal
  • 947
  • 1
  • 9
  • 30
0

Update: Go to app info and > Storage and clear data should worke as well.

One possible reason it worked first time but not second time could be that the database wasn't called in the first run?

Also from Marshmallow, Android app data is automatically backed up. for apps targeting 23+ so your app could have gotten the previous database from the cloud without you knowing.

Unfortunately, you can't yet delete app data individually. But if you don't mind killing your backed up data for all apps, you can uninstall the app then go to https://myaccount.google.com/dashboard and expand the Android section and delete backed up app data.

After that, you should be able to re-install the app and get a fresh new database.

This worked for me, but do so at your own risk.

Dittimon
  • 986
  • 2
  • 14
  • 28
0

This means that there is already a previous file and you are trying to create another one with the same name, you can change the name of the database, or on the device you are emulating uninstall the application, and emulate it again.

H.Josue
  • 31
  • 4
0

onDowngrade() is not overridden in your code. You should handle onDownGrade().

Brahem Mohamed
  • 342
  • 2
  • 5