1

I had a problem with updating of a column's value at a particular row. I had written

Cursor c = mDb.rawQuery("UPDATE "+book+" SET footnotes='" + note + "' WHERE chapter="+chapter+" and verse="+verse+"", null);
c.close();

But on adding c.moveToFirst() it worked. Why is that?

Cursor c = mDb.rawQuery("UPDATE "+book+" SET footnotes='" + note + "' WHERE chapter="+chapter+" and verse="+verse+"", null);
c.moveToFirst();
c.close();

Why is c.moveToFirst() necessary here, any particular reason?

There is an explation for c.moveToFirst() (What is The use of moveToFirst () in SQLite Cursors) which briefly suggests that using c.moveToFirst() does two things

allows you to test whether the query returned an empty set

moves the cursor to the first result

But how does the above two things help in updation?

Community
  • 1
  • 1
Jerry
  • 410
  • 6
  • 17
  • possible duplicate of [What is The use of moveToFirst () in SQLite Cursors](http://stackoverflow.com/questions/12445010/what-is-the-use-of-movetofirst-in-sqlite-cursors) – tim Aug 01 '15 at 19:28

1 Answers1

0

Think of rawQuery() as a wrapper for the C library sqlite3_prepare_v2() that compiles the SQL but does not run it, while think of moveTo..() as a wrapper for sqlite3_step() that is required for actually executing the prepared statement.

Related: What is the correct way to do inserts/updates/deletes in Android SQLiteDatabase using a query string?

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
  • more or less: `moveTo..()` is not a wrapper for anything, it just makes `onMove()` to be called which is implemented by `SQLiteCursor` and does the real stuff – pskink Aug 01 '15 at 21:20