Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite query with integer parameter

Tags:

android

sqlite

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?

like image 648
Saxon Druce Avatar asked Aug 31 '26 06:08

Saxon Druce


1 Answers

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.

like image 194
waqaslam Avatar answered Sep 01 '26 18:09

waqaslam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!