I have a string array and I want to query by where clause with string array.
Asked
Active
Viewed 1,604 times
-1
-
You need to transform your array into this string: `(1,4,16,18,20)` before using it in the query string. – juergen d Sep 19 '18 at 06:22
-
Now you made your question even worse. Add the code again in a well formatted manner – juergen d Sep 19 '18 at 06:24
-
https://stackoverflow.com/a/22577565/575376 – juergen d Sep 19 '18 at 12:18
1 Answers
3
You need to build a string of comma separated values from the array, not pass the array.
Assuming
String[] getAnswerIds = new String[]{"1","4","16","18","20"};
boolean after_first = false;
StringBuilder sb = new StringBuilder();
for (String s: getAnswerIds) {
if (after_first) {
sb.append(",");
} else {
after_first = true;
}
sb.append(s);
}
String your_list_of_values = sb.toString();
Then your_list_of_values will be the string 1,4,16,18,20 you then just need to place that string in the appropriate place within the query.
e.g.
Cursor mycursor = db.rawQuery("SELECT * FROM questions WHERE ID IN(" + your_list_of_values + ")",null);