0

This is the code I'm trying to run:

        open();
        database.beginTransaction();

        Cursor cursor = database.rawQuery("UPDATE message SET is_read=1 WHERE id = 'user1' AND (mid IN ('msg1','msg2'))",null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                <... success ...>
            }
            cursor.close();
        }
        database.endTransaction();
        close();

And it doesn't work - the response is successful but my cursor has no updated rows.

The same query against the same database work perfectly fine in pure SQL (I'm using sqlitebrowser). What's happening here?

vkislicins
  • 3,331
  • 3
  • 32
  • 62
  • try with AND mid IN ('msg1','msg2') – Vilas Jul 09 '15 at 12:12
  • why do you expect that `UPDATE` behaves like `SELECT` ? – Blackbelt Jul 09 '15 at 12:15
  • @EagleEye thanks for the suggestion - tried that before, tried again, same result. @Blackbelt All I expect is for `mUpdateRows` size inside cursor to be != 0 - if you're talking about moveToFirst - that's just something someone suggested in another post. I only need the count – vkislicins Jul 09 '15 at 12:23

2 Answers2

3

As this is an update, I recommend you change rawQuery for execSQL.

The difference between one and the other is detailed here: difference between rawquery and execSQL in android sqlite database

criscan
  • 91
  • 5
1

Try this, you need to set transaction successful after transaction completed.

Cursor cursor = database.rawQuery("UPDATE message SET is_read=1 WHERE id = 'user1' AND (mid IN ('msg1','msg2'))",null);
  if (cursor != null) {
        if (cursor.moveToFirst()) {
             <... success ...>
         }
         cursor.close();
        }
    database.setTransactionSuccessful();
    database.endTransaction();
    close();
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • A transaction must be marked as successful. If you don't do this all changes inside a transaction will be rolled back. https://www.sqlite.org/lang_transaction.html – jlhonora Jul 09 '15 at 12:35
  • if you call transactionSuccessful then only it commited otherwise it will be rollback. – Krishna V Jul 09 '15 at 12:39