Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite - named parameters

Tags:

android

sqlite

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

like image 571
Ivan Sivak Avatar asked Dec 01 '14 17:12

Ivan Sivak


2 Answers

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 });
like image 155
CL. Avatar answered Nov 11 '22 10:11

CL.


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);
like image 39
danny117 Avatar answered Nov 11 '22 10:11

danny117