4

The documentation for SQLiteDatabase.query says that the selectionArgs will be bound as strings.

What is the recommended way to perform a query with an integer parameter?

Is it this:

int value = 10;
Cursor cursor = database.query(
    "TABLE_X", 
    new String[] { "COLUMN_A", "COLUMN_B" },
    "COLUMN_C = " + value,
    null,
    null,
    null,
    null);

Or is it this:

int value = 10;
Cursor cursor = database.query(
    "TABLE_X", 
    new String[] { "COLUMN_A", "COLUMN_B" },
    "COLUMN_C = ?",
    new String[] { Integer.toString(value) },
    null,
    null,
    null);

Or something else?

0

1 Answer 1

2

Second option with ? is the suggested way of using query method.

selectionArgs: You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.

Sign up to request clarification or add additional context in comments.

Comments

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.