开发者

Android does not update SQLite database

开发者 https://www.devze.com 2023-03-31 21:21 出处:网络
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 d

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消