saeid ezzati
saeid ezzati

Reputation: 891

Error in rawQuery of sqlite

I have this query:

Cursor c = datasource.db.rawQuery("select * from posts where tags like '%?%' order by time desc",new String[]{arg0});

but it gives me this error:

Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters

I placed the ? character in my query but rawQuery() doesn't detect it.

What do I have to do now?

Upvotes: 0

Views: 750

Answers (2)

Phantômaxx
Phantômaxx

Reputation: 38098

like '%?%' - Since you enclosed the parameter placeholder in quotes, it's no longer being treated as a parameter, but as a string, literally.

You want to do so, instead:

Cursor c = datasource.db.rawQuery("select * from posts where tags like ?", new String[]{"%" + arg0 + "%"});

Upvotes: 4

Gabriella Angelova
Gabriella Angelova

Reputation: 2985

You could do two things. First:

Cursor c = datasource.db.rawQuery("select * from posts where tags like '%" + arg0 + "%' order by time desc", null);

Or second:

Cursor c = datasource.db.rawQuery("select * from posts where tags like'%' | '?' | '%'", new String[]{arg0});

here you could find more possible solutions https://code.google.com/p/android/issues/detail?id=3153

Upvotes: -2

Related Questions