Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Cursor is always returning null even if the database is not empty

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 ;
}      
like image 654
user2613911 Avatar asked Nov 02 '22 16:11

user2613911


1 Answers

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, ...);
like image 72
CL. Avatar answered Nov 11 '22 11:11

CL.