Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cursor.moveToNext without moveToFirst

Will the next code work as expected?

Cursor c = db.query(tableName, requestedColumns, condition,
        conditionParams, null, null, sortOrder);
while(c.moveToNext())   {
     //do stuff with rows
}

The examples I found so far suggest calling c.moveToFirst() prior looping, but is it really necessary?

like image 418
alandarev Avatar asked Mar 24 '14 14:03

alandarev


3 Answers

Yes, it will work, moveToNext actually call moveToFirst

like image 58
Tomer Mor Avatar answered Nov 11 '22 03:11

Tomer Mor


Yes, it will work, the first moveToNext() will point the cursor to the first entry of the result set, (if the result set has values)

like image 45
Guillermo Merino Avatar answered Nov 11 '22 02:11

Guillermo Merino


A late answer, but maybe useful to others finding this page. I've been using an: "if () do { } while();" loop. The "moveToFirst()" function returns false if no rows were returned in the cursor. The "moveToNext()" function returns false when past end of the cursor:

Cursor c = db.query(......);

if (c.moveToFirst()) do {
   data = c.getString(0) // eg. get value from first column in cursor
   // do something more
} while (c.moveToNext());

c.close();
like image 2
Stephen Bridgett Avatar answered Nov 11 '22 03:11

Stephen Bridgett