How To Check Cursor Null Or Not In Android?
I'm trying to check values of field ID in BangCauHoi table. This is my function: public boolean checkID(String ndCauHoi){ boolean check = true; String query = 'sele
Solution 1:
You can check to see whether or not it is empty with the following statement:
if (c.moveTofirst() && c.getCount > 0)
or
if(c!=null&&c.moveToFirst())
As to why it is empty, I would guess that you have mistyped a statement somewhere. I always use the query
method in the SQLiteDatabase
class instead of rawQuery
, and use constants for my column names instead of hardcoding them to try and avoid these problems.
publicstaticfinalString KEY_ID = "_id";
publicstaticfinalString TABLE_NAME="BangCauHoi";
publicstaticfinalString CAUHOI = "CauHoi";
Cursor c = yourDatabaseInstance.getReadabledatabase().query(
YourDatabaseName.TABLE_NAME,
newString[] { YourDatabaseName.KEY_ID },
YourDatabaseName.CAUHOI + "=?",
newString[] { ndCauHoi },
null, null, null);
Solution 2:
Your database does not have any rows mathing the selection and moveToFirst()
returns false. When unchecked, you'll get the exception about trying to retrieve values from a nonexisting row. When checked, it returns the default value true
you've specified.
Post a Comment for "How To Check Cursor Null Or Not In Android?"