I do not believe that you need to check if(cursor == null) {} .
First
If your query does not return any rows, you will get an empty Cursor. The cursor will not be null .
There are many ways to check if the cursor is empty:
if(cursor.getCount == 0) {}if(!cursor.moveToFirst()) {}
In fact, all cursor methods # moveTo ... () return either true or false , if you get false , then the line you requested does not exist.
Second
If an error occurs, you need to catch the error in the try-catch block, otherwise the application will crash from the unhandled exception.
Also insert() , update() and delete() return an integer, not a cursor. These methods return the number of lines affected by your statement; if no lines are affected, these methods return 0 .
source share