2

how can i use an Array of strings to query an sqlite database? i keep getting the exception "SQliteException: bind or column index out of range".

      String[] names = new String[values.size()]; // values is an Arraylist
      String[] condition = values.toArray(names);
Cursor row = db.query(true,DATABASE_TABLE, resultColumns, NAME +"=" + "?", condition, null, null, null,null);

please any help will be greatly appreciated as usual. Thanks

P.S values, which is the Arraylist is being poplulated through a loop so i can't have a defined size. thats why i can't use the get() method and seperate the elements in it by commas' which would have been easier.

1 Answer 1

5

Your query's where clause is where name = ?. This expects the passed-in array to be of size 1. Judging by its name (names), I'm guessing its not. You need to rethink what you want your SQL query to be (maybe where name in ...).

E.g.:

final String whereClause = NAME + " in (" + convertToCommaDelimitedString(values) + ")"; 
db.query(true,DATABASE_TABLE, resultColumns, whereClause, null, null, null, null,null);

You need to implement String convertToCommaDelimitedString(Collection c);

Sign up to request clarification or add additional context in comments.

4 Comments

yes.. you are right, the array size is more than one. but am not too good with sqlite.. so i don't know how to call that method you suggested. could you please give me the syntax format or a link i can follow along?... thanks for the response
hi.. thanks for the response.. i already have my Arraylist seperated by commas, so i don't know if i need to implement it again.
tried it.. its now giving me another sqliteException "syntax error".. :(
sorry, i forgot to tick the answer. had to re-implement my logic for my app and not it works. thanks once again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.