Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
505 views
Welcome To Ask or Share your Answers For Others

1 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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...