What is The use of moveToFirst () in SQLite Cursors What is The use of moveToFirst () in SQLite Cursors sqlite sqlite

What is The use of moveToFirst () in SQLite Cursors


The docs for SQLiteDatabase.query() say that the query methods return:

"A Cursor object, which is positioned before the first entry."

Calling moveToFirst() does two things: it allows you to test whether the query returned an empty set (by testing the return value) and it moves the cursor to the first result (when the set is not empty). Note that to guard against an empty return set, the code you posted should be testing the return value (which it is not doing).

Unlike the call to moveToFirst(), the test for if(c!=null) is useless; query() will either return a Cursor object or it will throw an exception. It will never return null.


if (c.moveToFirst()) {  while(!c.isAfterLast()) { // If you use c.moveToNext() here, you will bypass the first row, which is WRONG    ...    c.moveToNext();  } }


Cursor is not a Row of the result of query. Cursor is an object that can iterate on the result rows of your query. Cursor can moves to each row. .moveToFirst() method move it to the first row of result table.