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.

Leave a Comment