8

I'm working on Android application that uses SQLite as a local storage. I need to use parameters in sql query but all examples I have found contain unamed parameters, like so:

INSERT INTO SomeTable(ColA, ColB, ColC) VALUES (?,?,?);

I'm wondering - does SQLite on Android supports named parameters? Something like this instead of question marks..

INSERT INTO SomeTable(ColA, ColB, ColC) VALUES (@paramA, @paramB, @paramC);

SQLite itself supports this (according to the documentation https://www.sqlite.org/lang_expr.html).

Thanks in advance

1
  • 2
    if you take a look at the documentation for the SQL related classes, as far as i can tell, the query binding methods are all index-based. Commented Dec 1, 2014 at 17:22

2 Answers 2

8

The Android database API allows parameter binding only by index.

This does not prevent you from using named parameters in SQL, but you still have to use the correct index to bind them. This is pretty much useless, except for documentation, or for reusing a parameter:

db.rawQuery("SELECT * FROM Tab WHERE A = @search OR B = @search",
            new String[]{ search });
Sign up to request clarification or add additional context in comments.

2 Comments

Frankly, I'm a bit surprised since even the SQLite documentation discourages from using parameters binding by index. Anyway, thank you for the answer.
The Android database API contains lots of strange quirks. (Worst is that all functions except execSQL support only String parameters).
1

A more android SQLite specific way is to use a ContentValues for sql insert. In the example below. values is a ContentValues and it contains the column name and the value for the column. Columns not in the ContentValues are set to their default value on the insert.

            id = sqlDB.insertOrThrow(RidesDatabaseHandler.TABLE_RIDES,
                    null, values);

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.