5

Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.

Android doesn't recognise ? character in ?%

I try to achieve this:

 String selectQuery = "SELECT * FROM config WHERE Directory Like '?%';";

 SQLiteDatabase db = this.getReadableDatabase();
 Cursor cursor = db.rawQuery(selectQuery, new String[] {parent});

I have searched for a solution but couldn't find anything, I think it's pretty particullary. I am trying to do it like this because I want to make the query work with Strings that contains character '.

Maybe someone who had the same problem can answer me how can I solve this.

0

3 Answers 3

8

Use || to concat parameter and wildcard:

SELECT * FROM config WHERE Directory Like ? || '%';
Sign up to request clarification or add additional context in comments.

3 Comments

Still doesn't work :( same error "SELECT * FROM config WHERE Directory Like '?' || '%';"
@PavelMarian Try without ' around ?
Work's like a charm, going to accept the answer after the time limit. Didn't know I could concatenate the strings.
0
SELECT * FROM config WHERE Directory Like '?' || '%';

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, new String[] {parent});

using || solves this issue

Comments

-2

you can put what condition you have into a String variable and then pass it to your query like this--> String Condition="'?"; SELECT * FROM config WHERE Directory Like '"+Condition+"'

8 Comments

And allow SQL Injection attack
@lad2025: there are many ways that you can block SQL Injection attack.
The first rule is: Avoid building custom string, use parameter binding instead
@lad2025: sometimes you need to do your work in another way. but this do not mean you are mistake. just you are some flexible.
Good joke exposing to SQL attack Injection for being flexible.
|

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.