0

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.

Akash Agarwal
  • 2,326
  • 1
  • 27
  • 57

1 Answers1

0

You are not closing the connection to the database, and maybe the data is not actually written because of that. If you do not explicitly close the database connection, when you exit your application all connections will be closed, but this won't assure you the data to be stored successfully.

Add a db.close() statement after your db.endTransaction() line.

Good luck!

Juan
  • 118
  • 7
  • Strangely this never happened when I insert records. I read somewhere on stackoverflow that it is a good practice to leave DB connection open. – Akash Agarwal Apr 27 '16 at 05:45
  • @AkashAggarwal `I read somewhere on stackoverflow that it is a good practice to leave DB connection open.` I think it was an April's fool. – Phantômaxx Apr 27 '16 at 07:46
  • Maybe if you intend to keep using it for further DB operations. Otherwise It has no sense keeping it open. I close them always and it works fine. – Juan Apr 27 '16 at 14:42
  • Well if you use `db.close()` then it will throw can't operate on closed connection exception.. something like that. @BobMalooga Did you see that I mentioned in the question that the results get rolled back after app restart? while the app is running the results reflect like they should normally. And yet again, I never faced this problem with inserts – Akash Agarwal Apr 27 '16 at 17:48
  • Obviously enough, you have to re/open the db before using it. Then close it as soon as you used it. your mantra: `open the db, use it, close it`. – Phantômaxx Apr 27 '16 at 17:52
  • @BobMalooga: Could you please add a short pseudo that should be followed each function of my helper class to your answer? – Akash Agarwal Apr 27 '16 at 18:07
  • I never answered this question... ;) – Phantômaxx Apr 27 '16 at 18:54
  • Have you solved it yet? I think there could be a problem on naming your attribute `ui` identical as your table `ui`. So maybe your condition `ui = ?` is not doing what you think it should do. Try renaming the table or the attribute and tell us if it worked. – Juan Apr 29 '16 at 14:43
  • @BobMalooga : Refer this answer about our discussion about leaving DB open or not: http://stackoverflow.com/a/7999153/5157706 Juan: that can't be the issue because it worked before the app relaunches. Sorry for getting back this late guys! – Akash Agarwal May 11 '16 at 08:22
  • @AkashAggarwal My best option is following the mantra: `Open it, use it, close it`. – Phantômaxx May 11 '16 at 09:23