I'm trying to do a SQlite query in Android in which I try to do a parameterized WHERE clause with "?". Since I want to return all records if a parameter is selected as "Any", I try to implement the follow but don't seems to get the correct result. Can anyone please give me some hints on this?
String select = "SELECT * FROM person_table ";
String where = "WHERE first_name = ? AND gender = ? AND occupation = ?";
String query = select + where;
ArrayList<String> whereArgs = new ArrayList<String>();
if (!first_name.equals("Any"))
whereArgs.add(first_name);
else {
whereArgs.add("NULL");
}
if (!gender.equals("Any"))
whereArgs.add(gender);
else {
whereArgs.add("NULL");
}
if (!occupation.equals("Any"))
whereArgs.add(occupation);
else {
whereArgs.add("NULL");
}
String [] whereArgsStrArray = new String[whereArgs.size()];
whereArgsStrArray = whereArgs.toArray(whereArgsStrArray);
Cursor cursor = db.rawQuery(query,whereArgsStrArray);
As I have read from other post and searched on Google, it seems that if I pass a "null" string to the ? WHERE clause I can return all records from for that particular parameter. I have tried "NULL", "null" or null type in the whereArgs array but still couldn't get it working.
Thanks a lot. Lawrence
Aucun commentaire:
Enregistrer un commentaire