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

时间:2015-02-19 22:51:23

标签: android sqlite android-studio android-sqlite

我知道有很多关于此事的其他帖子,但无法找到答案。我正在关注github上的资产帮助指南HERE

我怀疑它是SQL语法错误

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要更新

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方法强制数据库更新来实现它的懒惰方式

1 个答案:

答案 0 :(得分:4)

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

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

这将首先创建可写数据库,升级数据库,然后您可以使用只读数据库。 请注意,运行此代码后,您可以删除这些行。