Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 565
Elad Benda2 Avatar asked Jun 05 '14 07:06

Elad Benda2


People also ask

How do I downgrade my database to the previous release?

Complete the following steps to downgrade your release 10.2 database to the Oracle Database release from which you originally upgraded: Log in to the system as the owner of the release 10.2 Oracle home directory. Note: This step is required only if the Enterprise Manager Database Control is already configured for the database.

What version of Oracle 9 I database can I downgrade to?

In Oracle Database 10 g release 10.2, downgrading is supported back to release 10.1 and release 9.2. However, if the release number of your Oracle9 i database is lower than release 9.2.0.6.0, then you should install the latest patch release for release 9.2; downgrading is not supported to releases prior to release 9.2.0.6.0.

How to downgrade the version of a table?

If you are trying to update your table, probably need to pass 3 not 2 and each and every time you need to update table, you need to increase the version (not decrease). If you really want to downgrade the version you can override the onDowngrade () as @Matthieu expressed in his answer.

Is ondowngrade() overridden in my code?

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. What are the possible reasons for version downgrade? what a question Muhammad Babar...!


2 Answers

Had the same problem and I solved it like this.

@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  db.setVersion(oldVersion);
}
like image 187
Balaji Ramavathu Avatar answered Sep 18 '22 17:09

Balaji Ramavathu


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

like image 33
Blasanka Avatar answered Sep 21 '22 17:09

Blasanka