Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite LIKE problem in android

Tags:

android

Hello i've spent almost 2 hours trying to figure out why the LIKE statement doesn't work and i only get this error: 03-03 11:31:01.770: ERROR/AndroidRuntime(11767): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x89d9f8

In SQLiteManager it works perfectly like this: SELECT Word FROM Sign WHERE Word LIKE 'he%'; But when i try to do it from java it won't work. Here's is my query, i've tried in a lot of ways with no luck:

Cursor cursor = m_db.query(MY_TABLE, new String[] {"rowid","Word"},"Word"+" LIKE '"+" ?"+"%'", new String[]{name}, null, null, null); 

Any ideas? i'm i doing it wrong or is there a bug?

Thanks for your time.

like image 812
madcoderz Avatar asked Mar 03 '11 10:03

madcoderz


People also ask

What is the alternative of SQLite in Android?

But if you want to replace SQLite completely, there are also quite a few alternative databases: Couchbase Lite, Interbase, LevelDB, Oracle Berkeley DB (formerly Oracle's mobile database was "Oracle Database Lite"), Realm, SnappyDB, Sparksee Mobile (graph database, brand-new at the time of this article), SQL Anywhere, ...

Can you use like in SQLite?

Introduction to SQLite LIKE operator Note that you can also use the LIKE operator in the WHERE clause of other statements such as the DELETE and UPDATE . The percent sign % wildcard matches any sequence of zero or more characters. The underscore _ wildcard matches any single character.

Is SQLite deprecated in Android?

In which case, does that mean that SQLLite is deprecated in Android? No.

Is SQLite still used in Android?

For Android, SQLite is “baked into” the Android runtime, so every Android application can create its own SQLite databases.


2 Answers

The solution is actually very easy. Just include the % inside your selectionArgs.

String []selectionArgs = {name + "%"}); 
like image 194
Jona Avatar answered Sep 18 '22 14:09

Jona


I think you shouldn't use selArgs for LIKE such a way. You may try this:

Cursor cursor = m_db.query(MY_TABLE, new String[] {"rowid","Word"},"Word"+" LIKE '"+name+"%'", null, null, null, null); 

EDIT:

OK, if you want be safe from SQL injections, don't use above solution, use this:

Cursor cursor = m_db.query(MY_TABLE, new String[] {"rowid","Word"},"Word LIKE '?'", new String[]{name+"%"}, null, null, null); 
like image 29
Olsavage Avatar answered Sep 17 '22 14:09

Olsavage