Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLiteAssetHelper Can't upgrade read-only database from version 1 to 2:

Tags:

android

sqlite

I want to create an offline dictionary app, that need sometimes to get update and old database will be replaced by new one. It's what I want to do, and I did something like this with SQLiteAssetHelper Library:

Note: SQLiteAssetHelper will copy database from assets folder into app data folder

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 database, after puting new db.sqlite file into assets folder, I have manipulate my codes like this:

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 run, it says: Can't upgrade read-only database from version 1 to 2

what is the solution?

by clearing app data, it will works fine...

SORRY FOR MY BAD ENGLISH...

like image 409
Omid Avatar asked Dec 06 '22 23:12

Omid


1 Answers

Quoting the documentation:

If you have a read-only database or do not care about user data loss, you can force users onto the latest version of the SQLite database each time the version number is incremented (overwriting the local database with the one in the assets) by calling the setForcedUpgrade() method in your SQLiteAsstHelper subclass constructor.

You can additionally pass an argument that is the version number below which the upgrade will be forced.

Note that this will overwrite an existing local database and all data within it.

You are not calling setForcedUpgrade() from your constructor.

like image 178
CommonsWare Avatar answered Dec 10 '22 11:12

CommonsWare