Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL parameterized queries in Android

I've been looking over what interfaces/classes exist in Android for parametarized queries and it's a bit of a mess.

Can someone make it clear what is the best (and safest) among:

  • using SQLiteDatabase.query()
  • using SQLiteDatabase.rawQuery()
  • using SQLiteStatement
  • using SQLiteQueryBuilder
  • using compileStatement()
  • using managedQuery()
  • using ContentValues
  • using something else, e.g. execSQL()

Can anyone explain why so many exist, what's the difference between them and what's the best way to have bind parameters for querys?

like image 405
frustrated_george Avatar asked Feb 08 '12 17:02

frustrated_george


People also ask

What is parameterized query in android?

A parameterized query is a query in which placeholders are used for parameters and the parameter values are supplied at execution time. The most important reason to use parameterized queries is to avoid SQL injection attacks.

What are parameterized SQL queries?

Parameterized SQL queries allow you to place parameters in an SQL query instead of a constant value. A parameter takes a value only when the query is executed, which allows the query to be reused with different values and for different purposes.

Can I use SQL in Android Studio?

Android App Development for Beginners SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.

What is @query in Kotlin?

Kotlin |Java. @Target([AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER]) annotation class Query. androidx.room.Query. Marks a method in a Dao annotated class as a query method. The value of the annotation includes the query that will be run when this method is called.


1 Answers

I've been looking over what interfaces/classes exist in Android for parametarized queries and it's a bit of a mess.

Not really.

Can someone make it clear what is the best (and safest) among:

No, because you have not defined what you think is the criteria for "best".

using SQLiteDatabase.query() using SQLiteDatabase.rawQuery()

Personally, of these, I use rawQuery(), as I find it to be the most readable. IMHO, query() is more for cases where you are trying to construct a SELECT statement from individual pieces (e.g., table name, list of columns), because perhaps some of it is variable (e.g., different tables for different users). At the end of the day, they both do the same thing. query() uses SQLiteQueryBuilder under the covers (see below).

using SQLiteStatement using compileStatement()

These are the same thing. SQLiteStatement is what Java refers to as a "class". compileStatement() is what Java refers to as a "method". The compileStatement() method returns an instance of the SQLiteStatement class.

SQLiteStatement is not usually that useful for queries, as it cannot return complete result sets, except for single-column/single-row responses.

using SQLiteQueryBuilder

This is another way of constructing a query from individual pieces. It is useful in cases where multiple parties get to decide what goes in the query, such as a ContentProvider and a consumer of that ContentProvider. Beyond that, though, in the end, it executes a rawQuery(). Hence, there is no significant difference in results from using SQLiteQueryBuilder or using rawQuery() directly.

using managedQuery()

This is deprecated and is not related to SQLiteDatabase in any event.

using ContentValues

This has nothing to do with queries against a SQLiteDatabase.

Can anyone explain why so many exist

Some serve different roles, in many cases having nothing to do with queries against a SQLiteDatabase.

With respect to the three valid options out of your roster (rawQuery(), query(), and SQLiteQueryBuilder), they all do the same thing: query() uses SQLiteQueryBuilder, and SQLiteQueryBuilder uses rawQuery(). The only difference is in how the SQL SELECT statement is constructed.

like image 160
CommonsWare Avatar answered Oct 21 '22 09:10

CommonsWare