Marian Pavel
Marian Pavel

Reputation: 2876

Cannot use Like statement in Android SQLite with argument and % character

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.

Upvotes: 5

Views: 1092

Answers (3)

Sathish Kumar
Sathish Kumar

Reputation: 957

SELECT * FROM config WHERE Directory Like '?' || '%';

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

using || solves this issue

Upvotes: 0

karimkhan
karimkhan

Reputation: 329

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+"'

Upvotes: -2

Lukasz Szozda
Lukasz Szozda

Reputation: 175706

Use || to concat parameter and wildcard:

SELECT * FROM config WHERE Directory Like ? || '%';

Upvotes: 8

Related Questions