Android SQLiteAssetHelper Unable to update read-only database from version 1 to 2:

I want to create an offline dictionary application, which is sometimes required for updating, and the old database will be replaced with a new one. This is what I want to do, and I did something like this with the SQLiteAssetHelperLibrary:

Note. will copy the database from the data folder to the application data folder SQLiteAssetHelper

public class MyDb extends SQLiteAssetHelper {
  private static final String DATABASE_NAME    = "db.sqlite";
  private static final int    DATABASE_VERSION = 1;
  public MyDb(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
}

Now I want to update the database by adding a new db.sqlitefile to the folder assets, I manipulate my codes as follows:

public class MyDb extends SQLiteAssetHelper {
  private static final String DATABASE_NAME    = "db.sqlite";
  private static final int    DATABASE_VERSION = 2;
  public MyDb(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
}

but when I compiled and started, it says: It is not possible to update a read-only database from version 1 to 2

what's the solution?

clearing the application data, it will work fine ...

AVOID FOR MY BAD ENGLISH ...

+4
4

:

, , , SQLite , ( , ), setForcedUpgrade() SQLiteAsstHelper.

, , .

, .

setForcedUpgrade() .

+6

setForcedUpgrade(); :

public class MyDb extends SQLiteAssetHelper {

  private static final int    DATABASE_VERSION = 2;//+1
  public MyDb(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    setForcedUpgrade(); 
  }
}
+6

:

  @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion != newVersion) {
      context.deleteDatabase(DATABASE_NAME);
      new MyDb(context);
    }else
      super.onUpgrade(db, oldVersion, newVersion);
  }

, .

+1

, db onUpgrade . . 1, .

The correct way is to use onUpgrade to change the schema. Most of the time, changing the schema, you need to recreate the table. In such cases, below are the approaches

  • rename an existing table (say table_xyz), which will be changed to some temperature name (say temp_table_xyz)
  • create a table (table_xyz) with a new schema
  • copy content from temp table temp_table_xyz to new table_xyz
+1
source

All Articles