Skip to content
Advertisement

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?

Advertisement

Answer

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;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement