This is a pretty simple question. I hope the answer is βboth,β but I don't bother either. I carefully studied the Android developer docs for SQLiteDatabase and Cursor , but cannot find a definitive answer to this question.
The case I'm asking is where I get the cursor, call moveToFirst
, and then loop until isAfterLast
returns true. It would be very convenient for a template that I code several times if it just worked and executed a loop 0 times if the cursor has 0 records.
Or do I need to explicitly check for an empty cursor?
EDIT: Some of the answers show that people do not quite get what I ask. Basically, I would like to write the following:
cursor = myDb.query(...) cursor.moveToFirst() while (!cursor.isAfterLast()) { processRow(...) }
But I'm not 100% sure that I can. isAfterLast
cannot return anything useful, but true for this case, but that does not mean that it really returns true for an empty request. The docs don't seem to indicate what the return value of most cursor methods is if the cursor is empty (in fact, Android docs look nonspecific in many cases with corners) other than getCount
. So I'm worried that I need to do this:
cursor = myDb.query(...) if (cusror.getCount() > 0) { cursor.moveToFirst() while (!cursor.isAfterLast()) { processRow(...) } }
This is random and logically redundant. Keep in mind that in a real implementation there is more code and this extends to several methods. And one of the answers now suggests that I need to also check the cursor for null
, which is completely undocumented ...
I just want to know if anyone knows about the actual behavior of isAfterLast
when calling an empty cursor.
source share