Android SQLite named parameters

I am working on an Android application that uses SQLite as local storage. I need to use parameters in a sql query, but all the examples I found contain unnamed parameters, for example:

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

I am wondering - does SQLite on Android support 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 ).

Thank you in advance

+6
source share
2 answers

The Android database API allows you to bind parameters only by index.

This does not stop 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 parameter reuse:

 db.rawQuery("SELECT * FROM Tab WHERE A = @search OR B = @search", new String[]{ search }); 
+5
source

A more specific way for Android-SQLite is to use ContentValues ​​to insert sql. In the example below. values ​​is ContentValues ​​and contains the column name and value for the column. Columns not contained in ContentValues ​​have a default value for insertion.

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

Source: https://habr.com/ru/post/978951/


All Articles