Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - sqlite in clause using values from array

I want to execute a sqlite query:

select * from table_name where id in (23,343,33,55,43);

The values in the in clause need to be taken from an array of strings:

String values[] = {"23","343","33","55","43"}

How can I achieve that?

like image 709
Udi Idan Avatar asked Feb 08 '12 23:02

Udi Idan


1 Answers

I believe a simple toString() will mostly do the trick:

String values[] = {"23","343","33","55","43"};
String inClause = values.toString();

//at this point inClause will look like "[23,343,33,55,43]"
//replace the brackets with parentheses
inClause = inClause.replace("[","(");
inClause = inClause.replace("]",")");

//now inClause will look like  "(23,343,33,55,43)" so use it to construct your SELECT
String select = "select * from table_name where id in " + inClause;
like image 129
Sam Dozor Avatar answered Oct 12 '22 08:10

Sam Dozor