Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite - on downgrade

Recently I updated my android game, editing sqlite database adding new field in my table, after update, I received 4 crash reports (3 of them comes from same device, Samsung Galaxy S4)

android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1 at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:361) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:255) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)

I really can not understand what might be the reason, when it could be even possible to start downgrading database, while it actually needed to upgrade in my last database.

During update, I have obviously increased DATABASE_VERSION value from 1 to 2

I am not overriding onDowngrade method, my onUpgrade looks like following:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    updateToVersion2(db, oldVersion, newVersion);
}

private void updateToVersion2(SQLiteDatabase db, int oldVersion, int newVersion)
{
    if (oldVersion <= 1)
    {
        ContentValues cv1 = new ContentValues();
        cv1.put(FIELD_WORLD_ID, 6);
        cv1.put(FIELD_MAX_UNLOCKED, 1);
        db.insert(TABLE_LEVELS, null, cv1);
    }
}

Thank you.

like image 621
Matthewek Avatar asked Nov 05 '13 10:11

Matthewek


People also ask

Is SQLite deprecated?

In which case, does that mean that SQLLite is deprecated in Android? No.


1 Answers

The problem is that you are not overriding the onDowngrade method. As explained in the javadoc, you need to, or you will get an exception:

If not overridden, default implementation will reject downgrade and throws SQLiteException

like image 200
Francis Avatar answered Sep 20 '22 20:09

Francis