mardi 26 avril 2016

Android SQLite update gets rolled back on app relaunch

I'm trying to perform an update on a table which contains only 1 row. The update gets performed successfully but as soon as I relaunch the app, the update gets rolled back!
Here is my code:

public void updateToken(String token) {
    System.out.println("DatabaseHandler.updateToken");
    System.out.println("Value of token: " + token);
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("Select * from ui", null);
    String data = "";
    while(cursor.moveToNext()){
        for(int i= 0; i<cursor.getColumnCount(); i++) {
            data += cursor.getString(i) + " | ";
        }
        data += "\n";
    }
    Log.i("UI Table before update",data);

    ContentValues values = new ContentValues();
    values.put("gcmtoken", token);
    db.beginTransaction();
    int r = db.update("ui", values, "ui = ?", new String[]{"" + getUI()});
    System.out.println("Number of rows affected: " + r);
    System.out.println("Updated GCMToken in DB: " + getGCMToken());
    cursor = db.rawQuery("Select * from ui", null);
    data = "";
    while(cursor.moveToNext()){
        for(int i= 0; i<cursor.getColumnCount(); i++) {
            data += cursor.getString(i) + " | ";
        }
        data += "\n";
    }
    Log.i("UI Table after update",data);
    db.setTransactionSuccessful();
    db.endTransaction();
}

The corresponding output shows the previous token in first Log.i() and new token in second Log.i() on every launch of app.

Aucun commentaire:

Enregistrer un commentaire