5

Cursor is always returning null even if the database is not empty. Why? and how to solve that? Thank you.

 public StatParcours getOneUserInfo(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_USER, new String[] { KEY_USER_ID,
            KEY_STUDN , KEY_STUDBD, KEY_TEACHN, KEY_TEACHBD}, KEY_USER_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);

    StatParcours stat = null;
    if ((cursor!= null)&&(cursor.moveToFirst())){      
        stat = new StatParcours(
            Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), 
            cursor.getString(2),
            cursor.getString(3),
            cursor.getString(4));
    }  
    return stat ;
}      
10
  • Are you sure the cursor is null? Perhaps it just doesn't have any rows? Have you checked that the query you are making will return at least 1 result? Commented Aug 10, 2013 at 23:24
  • Yes, I have checked the database content using Log, than displayed value of Stat when I call this method, witch is always null. So understood that cursor always null. Commented Aug 10, 2013 at 23:28
  • If you do Log.d("Foo", "Cursor is:" + cursor); does that print Cursor is: null? If that does not print null then cursor is empty. Empty means your query has found 0 elements that match the criteria and you may need to check if your query is wrong. Commented Aug 10, 2013 at 23:59
  • Its printing: Cursor is:android.database.sqlite.SQLiteCursor@428085b0 Commented Aug 11, 2013 at 0:09
  • 2
    So your cursor is not null, it is empty. That means that there is nothing in your database that matches KEY_USER_ID + "=?". Check the value of id and you should see that there is nothing with exactly that value. Commented Aug 11, 2013 at 0:35

3 Answers 3

2

You are comparing the user ID against a string value, which will never succeed if your user IDs are numbers.

In Android, you should use query parameters only for string values; you should embed integers directly into the query string:

cursor = db.query(..., ..., KEY_USER_ID + "=" + id, null, ...);
Sign up to request clarification or add additional context in comments.

Comments

0

you should enture the cursor is null or not and if not, you need confirm the cursor.getCount() is not zero, are you sure?

if (cursor!= null && cursor.getCount() > 0 && cursor.moveToFirst()){
    //do something
}

if cursor is null or cursor.getCount() is zero, but database exist and data is correct, you should detect your sql and run command in terminal or sql client to fix this issue.

2 Comments

Checking for null is not necessary, and the getCount and moveToFirst checks are redundant.
people keep saying checking for cursor==null is not necessary, the reason is that the case where cursor is null, query will have thrown an exception, so you won't get to the check.
0

The CursorFactory value when create the database should be null if you don't have specific requirement.

That's how I solved this problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.