Matthewek
Matthewek

Reputation: 1559

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.

Upvotes: 6

Views: 5548

Answers (1)

Francis
Francis

Reputation: 1090

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

Upvotes: 3

Related Questions