java - One parameter with multiple arguments - Selection and SelectionArgs -
this question has answer here:
- preparedstatement in clause alternatives? 25 answers
i have encountered problem:
caused by: java.lang.illegalargumentexception: cannot bind argument @ index 3 because index out of range. statement has 1 parameters.
as result of:
string = "id_1 = 50 , id_2 = ?"; //the string[] ideally of dynamic length , not 3 in size. string[] whereargs = {"60", "61", "62"}; cursor.setselection(where); cursor.setselectionargs(whereargs);
i using wrong way. have realized already. think makes evident trying accomplish.
my question: there way me insert array of varying length , arguments single parameter? best practices have missed. have baked myself bad situation?
the sql statement looking for:
where id_1 = 50 , ((id_2 = 60) or (id_2 = 61) or (id_2 = 62))
the way can think of fix problem create string , build upon in loop length of string[] adding or (id_2 = xx) every iteration. doesn't sound solution me.
thanks time!
in general, constructing clause dynamically correct solution. use stringbuilder.append()
rather +
save string construction overhead.
in cases, may want issue multiple statements. instead of
select table id_1 = 50 , (id_2 = ? or id_2 = ? or id_2 = ?);
you batch of
select table id_1 = 50 , (id_2 = ?); select table id_1 = 50 , (id_2 = ?); select table id_1 = 50 , (id_2 = ?);
Comments
Post a Comment