Issue
I don't seem to find any information on query, insert or any other SQL method returning null. But it does if error occurs.
I just wonder whether cursor being null means an error occurred or could it mean no rows were selected (for instance)? I don't know how I should treat it - as an error or something that may happen time to time.
Solution
I don't believe you ever need to check if(cursor == null) {}
.
First
If your query doesn't return any rows, you will receive an empty Cursor. The Cursor will not be null
.
There are many ways to check if a Cursor is empty:
if(cursor.getCount == 0) {}
if(!cursor.moveToFirst()) {}
In fact all of the Cursor#moveTo...() methods return either true
or false
, if you receive false
then the row that you requested does not exist.
Second
If an error occurs then you need to catch the error in a try-catch block, otherwise the app will crash from an unhandled exception.
Also insert()
, update()
, and delete()
return an integer, not a Cursor. These methods return the number of rows affected by your statement, if no rows are affected these methods return 0
.
Answered By - Sam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.