0

I implemented a ContentProvider for my android app and it works very well but when I make some changes to the schema, the database doesn't seem to be upgraded. I have the following code in my SQLiteOpenHelper :

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(DEBUG_TAG, "Upgrading database. Existing contents will be lost. ["
        + oldVersion + "]->[" + newVersion + "]");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TUTORIALS);
    onCreate(db);
}

If I update my app with a new schema then all my inserts fail BUT if I uninstall the old version and install the new version of the app then everything works fine. So I guess that onUpgrade() is never called. What do oldVersion and newVersion refer to? Do I need to specify the version anywhere?

1 Answer 1

2

SQLiteOpenHelper has a version argument, if you increment onUpgrade should be called:

public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

For example I have the following class and if I increment dbVersion it is called:

public class DBHelper extends SQLiteOpenHelper {

    private SQLiteDatabase m_Db;

    public DBHelper(Context context, String dbPath, int dbVersion) {

        super(context, dbPath, null, dbVersion);
        ...
        m_Db = getWritableDatabase();
    }
    ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.