Can't downgrade database from version 2 to 1 even after fresh install and re-run
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.
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).
Had the same problem and I solved it like this.
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.setVersion(oldVersion);
}