Android SQLite query with integer parameter

The documentation for SQLiteDatabase.query says it selectionArgswill be bound as strings.

What is the recommended way to execute a query using an integer parameter?

It:

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 like 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?

+3
source share
1 answer

The second option with ?is the proposed way to use the query method .

selectionArgs . Can you turn on? s to the selection, which will be replaced by the values ​​from selectionArgs so that they appear in the selection. values ​​will be linked as strings.

+2
source

All Articles