How To Test If Cursor Is Empty in a SQLiteDatabase Query How To Test If Cursor Is Empty in a SQLiteDatabase Query database database

How To Test If Cursor Is Empty in a SQLiteDatabase Query


What about testing the cursor like this, and then doing what you've said:

if(cursor!=null && cursor.getCount()>0)

getCount ()

Returns the numbers of rows in the cursor

http://developer.android.com/reference/android/database/Cursor.html#getCount()


The easiest and cleanest way to test for an empty cursor is the following code:

if ( cursor.moveToFirst() ) {    // start activity a} else {    // start activity b}

Per the docs, the method returns false if the cursor is empty:

http://developer.android.com/reference/android/database/Cursor.html#moveToFirst%28%29

public abstract boolean moveToFirst ()

Added in API level 1 Move the cursor to the first row.

This method will return false if the cursor is empty.

Returns whether the move succeeded.


You just need to use getCount().If your sql is correct but doesn't return any row you will have a NOT null cursor object but without a rows and getCount() will return 0.