Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Android database cursor implemented in details?

I'm interesting about the implementation details of the Cursor in Android. I know that basically it is just an interface that provides random read-write access to the result set returned by a database query. I wonder about the particular Cursor implementations:

  1. Is it a some kind of data structure which stores result set from database?
  2. Or it is just a structure which handles only one row?
like image 206
Oleksandr Karaberov Avatar asked Mar 20 '13 09:03

Oleksandr Karaberov


1 Answers

Is is just some data structure which stores result set from database

Yes.

Or it is some structure which handles only one row?

Not exactly. Cursor can handle one or more rows based on source query. Cursor is map of data and will contain as many rows as source(select) query returns. So if query returns hundred rows, these rows are inserted to Cursor and Cursor contain these hundred rows.

You can imagine Cursor as map of data with dynamic size(similar as List) where data are placed as "rows". Each row has own row id(row number, we can say some pointer) that is generated gradually as they are inserted to Cursor and you can simply move between rows with this number. Implicitly each Cursor has "actual pointer" positioned before first row(-1 position) so if you call c.getString(0) expection will be thrown because there is nothing to retrieve.

So always you have to call cursor.moveToFirst() that prepare Cursor for reading and if is empty, method return false because there is no row.

like image 134
Simon Dorociak Avatar answered Sep 30 '22 10:09

Simon Dorociak