SQLITE cant upgrade read-only database from versio

2020-07-27 07:15发布

I know there's loads of other posts about this but cant find an answer. I'm following the asset helper guide on github HERE

I suspect its an SQL syntax error

LOG

02-19 22:26:18.071  28023-28023/com.calvert.adam.lolinformer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.calvert.adam.lolinformer, PID: 28023
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.calvert.adam.lolinformer/com.calvert.adam.lolinformer.championsActivity}: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 1 to 2: /data/data/com.calvert.adam.lolinformer/databases/champions.db
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 1 to 2: /data/data/com.calvert.adam.lolinformer/databases/champions.db
        at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:266)
        at com.calvert.adam.lolinformer.myDBHelper.getChampions(myDBHelper.java:19)
        at com.calvert.adam.lolinformer.championsActivity.onCreate(championsActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

            

SQL To update

ALTER TABLE Champions RENAME TO Champions_ME_TMP;
CREATE TABLE Champions (
_id int NOT NULL AUTO_INCREMENT,
champ_splash blob NOT NULL,
Name TEXT(20) NOT NULL,
PRIMARY KEY (_id)
);
INSERT INTO Champions  ("_id", "champ_splash", "Name") SELECT "_id", "champ_splash", "Name" || ' ' || "Name" FROM "Champions_ME_TMP";
DROP TABLE Champions_ME_TMP;

DBHelper Class

 public class myDBHelper extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "champions.db";
    private static final int DATABASE_VERSION = 2;

    public myDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public Cursor getChampions() {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String [] sqlSelect = {"0 _id", "Name"};
        String sqlTables = "Champions";

        qb.setTables(sqlTables);
        Cursor c = qb.query(db, sqlSelect, null, null,
                null, null, null);

        c.moveToFirst();
        return c;

    }


}

changing the version number auto calls the assethelper to trigger the update

EDIT

I've just done it the lazy way by calling the setForcedUpgrade method to force the database to update

1条回答
▲ chillily
2楼-- · 2020-07-27 07:32

The database is trying to update it's version, but since upgrading is a writing operation it can not be done on the read only database you are requesting, hence the error.

public Cursor getChampions() {
    SQLiteDatabase db = getWritableDatabase();
    db.close();
    db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    ....
}

This will create a writable database first, upgrade the database, and then you can use a read-only database. Note that after running this code you can remove those lines.

查看更多
登录 后发表回答