Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android does not update SQLite database

I am experiencing some trouble with an SQLIte database in my Android application. The issue is that the database is never updated, not even on multiple restarts of the emulator, of Eclipse or after deletion from DDMS.

This is my onCreate method, located in a class that extends SQLiteOpenHelper:

public void onCreate(SQLiteDatabase database) {
    try {
        database.execSQL(ESSENCE_TABLE_CREATE);
        database.execSQL(PACCO_TABLE_CREATE);
        database.execSQL(TAVOLE_TABLE_CREATE);
        database.rawQuery("insert into essenza values(1, 'Rovere')",
                null); // added later
    } catch (SQLException e) {
        Log.e("DB", e.getMessage());
    }
}

After instantiating the helper, I request a reference to the database:

    helper = new DBHelper(context, dbpath + "/" + DATABASE_NAME);
    database = helper.getWritableDatabase();

It seems that the rawQuery statement (which was added at a later time) is not executed and that the database in use is instead cached from a previous version. I also tried to change the version of the database, but it did not work. Am I missing something? Thanks in advance.

like image 447
Tilvia Avatar asked Sep 13 '25 02:09

Tilvia


1 Answers

You have two options:

  1. Use DDMs to delete the database file from your device (look in /data/data/). This will force Android to run onCreate again.
  2. In your constructor, increment the database version you pass to SQLiteOpenHelper. Add your raw query to onUpgrade.

You probably want option 1. Option 2 is better if you have users of your app whose databases you want to update.

like image 155
Eric Levine Avatar answered Sep 15 '25 15:09

Eric Levine