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
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).
This exception happens when the database needs to be downgraded, but your SQLiteOpenHelper-derived class does not implement the onDowngrade callback.
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
Had the same problem and I solved it like this.
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.setVersion(oldVersion);
}
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.
This exception is thrown on the following conditions:
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.