Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite: looks like moveToNext works without moveToFirst needed

Tags:

android

sqlite

After creating a standard SQLite cursor, I'm iterating thru the entries using:

while (cursor.moveToNext()) {
}

All the rows are being processed correctly. All the documentation I've read states you need to issue a moveToFirst() to ensure that you're pointing to the first entry of the cursor set.

Is this working even though it shouldn't, and another release may not have the same processing?

like image 592
charlest Avatar asked Aug 21 '11 19:08

charlest


People also ask

What does cursor moveToFirst do?

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).

What is cursor moveToNext ()?

moveToNext() Move the cursor to the next row.

What is the purpose of the cursor class?

Cursors are what contain the result set of a query made against a database in Android. The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that were returned from the query as well as iterate over the rows of the result set.


1 Answers

No, this is working correctly. Cursors begin at row index -1 (before the first row). If the Cursor references multiple rows, looping through them with the while loop as you have suggested is the preferred method. It will call moveToNext(), which moves you to index 0 (the first row), and go from there.

If your Cursor only references one row, you may call moveToFirst() on it before reading data to ensure that you are on a valid index. Both moveToFirst() and moveToNext() have the same effect when the Cursor is first created and is at index -1.

like image 117
Glendon Trullinger Avatar answered Oct 02 '22 14:10

Glendon Trullinger