jeudi 17 septembre 2015

Update specific table when onUpgrade with SQLite with provided database

Probably there is somewhere here answer to my question, but if it exist I can't find it. Here is my problem: I have provided database in assets folder. When I want to update my app because I put some more rows in specific table I increase db version, call this.getWritableDatabase(); and onUpgrade is called, to this point everything is ok. But what next? Here is my onUpgrade function:

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
    if(i < i2) {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,SQLiteDatabase.OPEN_READWRITE);
        db.execSQL("attach database ? as newdb", new String[]{DB_PATH + DB_NAME}); // I have to attach new version of db to the old one and then i can go on
        db.execSQL("delete from GAME");
        db.execSQL("INSERT INTO GAME SELECT * FROM newdb.GAME");
        db.close();
}

This gives me an error that my db is locked - on the line:

db.execSQL("INSERT INTO GAME SELECT * FROM newdb.GAME");

I tried a lot of different scenarious but I just can't make it. I'll be very glad if someone could help me make it works ;)

Aucun commentaire:

Enregistrer un commentaire