SQLITE 无法将只读数据库从版本 1 升级到版本 2

SQLITE cant upgrade read-only database from version 1 to 2

我知道有很多关于此的其他帖子,但找不到答案。我正在关注 github HERE

上的资产帮助指南

我怀疑这是一个 SQL 语法错误

日志

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.access0(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待更新

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;

    }


}

更改版本号会自动调用 assethelper 来触发更新

编辑

我刚刚通过调用 setForcedUpgrade 方法来强制更新数据库,以懒惰的方式完成了它

数据库正在尝试更新其版本,但由于升级是写入操作,因此无法在您请求的只读数据库上完成,因此出现错误。

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

这会先创建一个可写数据库,升级数据库,然后就可以使用只读数据库了。 请注意,在 运行 这段代码之后,您可以删除这些行。