Unable to downgrade the database from version 2 to 1 even after a new installation and restart

I am writing an Android application using SQLite DB.

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

Then my database schema became stable because I didn’t release the application and it for my own use

I decided to change the version to 1 again.

I did a new install and everything worked fine.

But then starting a second time causes 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 this, in the end, I made a new installation, and the DB also needed to be deleted. Not?

How can I change the version to 1 again?

+11
java android database sqlite
source share
10 answers

This exception is thrown under the following conditions:

  • On the device on which the code is running, there is a database file version 2.
  • The code asks for version 1 of the database (with a parameter for the SQLiteOpenHelper constructor)
  • onDowngrade() not overridden in your code.

You say that the code worked fine the first time after a new installation. Make sure there is no other code that again increases the version number of the same database file to 2.

+17
source share

You can override onDowngrade() as you wish if you want to be able to run your application with the database on a 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 as well .

Without overriding this method, we will not be able to reduce the version of SQLite after the increase and execution. So you need to change back to 2 or more than 2 :

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

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

+1
source share

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

0
source share

You do not show where the database is being created, where the database version is usually assigned (i.e. SQLiteOpenHelper ctor).

Is it likely that you have a database on an external file system? If you do not follow the naming conventions, these files are not deleted when the application is uninstalled.

0
source share

How did you complete the new installation?

  • adb install -r ? In this case, the data of your application, including the database (s), will not be affected.
  • adb uninstall and adb install ? This would be the right way to perform a clean installation. All data will be deleted during uninstall , and your database will only be created after the first run after install .

But you write that during the first launch after a new installation, it worked. This may mean that you are upgrading to version 2 somewhere else.

0
source share

Update : Go to the application information and> Storage and clear data should also work.

One of the possible reasons why he worked the first time, but not the second time, may be because the database was not called on the first start?

Also from Marshmallow, Android app data is automatically saved. to target 23+ applications so that your application can get the previous database from the cloud without knowing.

Unfortunately, you cannot delete application data separately yet. But if you do not mind killing your backup data for all applications, you can delete the application, then go to https://myaccount.google.com/dashboard and expand Android and delete the backup data of the application.

After that, you can reinstall the application and get a new new database.

It worked for me, but do it at your own peril and risk.

0
source share

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

0
source share

There was the same problem, and I solved it like that.

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

onDowngrade () is not overridden in your code. You must process onDownGrade ().

0
source share

download sqlite db file from Android DeviceFileExplorer

[data / data / name of your package / database]

open file with ( DB Browser for SQLite )

go to the " Edit pragmas " tab and downgrade the database version from the user version

finally save the file and upload it to DeviceFileExplorer

enter image description here

0
source share

All Articles