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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With