kibibyte
kibibyte

Reputation: 3037

Getting a SQLiteException and I'm not sure what's causing it

My Android app is crashing with this error:

E/AndroidRuntime(  315): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x3470a0
...
E/AndroidRuntime(  315):        at lee.medical.icu.dataentry.db.PatientInfoDbHelper.getTests(PatientInfoDbHelper.java:163)

It's stumbling on this bit of code:

SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] columns = {C_LOCATION, C_TESTS};
String selection = "? = '?' and ? = '?'";
String[] selectionArgs = {C_LAST, lastName, C_FIRST, firstName};
Cursor cursor = db.query(TABLE, columns, selection, selectionArgs,
        null, null, null); // line 163

What could this error mean?

(A couple of notes: C_LOCATION, C_TESTS, C_LAST, and C_FIRST are columns in my database. lastName and firstName are method arguments that are self-explanatory.)

Edit: I had to play around with antlersoft's solution a bit to make it work. In case anyone else having the same problem stumbles upon this question, the solution in my case was to change the selection to:

String selection = C_LAST + " = ? and " + C_FIRST + " = ?"

Upvotes: 1

Views: 417

Answers (1)

antlersoft
antlersoft

Reputation: 14786

Don't put your ? in single-quotes for string arguments; in single quotes the ? is taken as a literal string and not as an argument placeholder-- SQLite will know the type of the argument passed as ? and so you don't have to quote it.

The way you have written it there are four arguments and only two argument placeholders in your query and so the exception.

Upvotes: 4

Related Questions