Reputation: 79
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 ;
}
Upvotes: 5
Views: 10064
Reputation: 879
The CursorFactory value when create the database should be null if you don't have specific requirement.
That's how I solved this problem.
Upvotes: 0
Reputation: 180020
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, ...);
Upvotes: 2
Reputation: 369
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.
Upvotes: 0