Added in API level 1

SQLiteQueryBuilder


open class SQLiteQueryBuilder
kotlin.Any
   ↳ android.database.sqlite.SQLiteQueryBuilder

This is a convenience class that helps build SQL queries to be sent to SQLiteDatabase objects.

This class is often used to compose a SQL query from client-supplied fragments. Best practice to protect against invalid or illegal SQL is to set the following:

Summary

Public constructors

Public methods
open static Unit

Add the names that are non-null in columns to s, separating them with commas.

open Unit

Append a chunk to the WHERE clause of the query.

open Unit

Append a chunk to the WHERE clause of the query.

open Unit

Add a standalone chunk to the WHERE clause of this query.

open String!
buildQuery(projectionIn: Array<String!>!, selection: String!, groupBy: String!, having: String!, sortOrder: String!, limit: String!)

Construct a SELECT statement suitable for use in a group of SELECT statements that will be joined through UNION operators in buildUnionQuery.

open String!
buildQuery(projectionIn: Array<String!>!, selection: String!, selectionArgs: Array<String!>!, groupBy: String!, having: String!, sortOrder: String!, limit: String!)

open static String!
buildQueryString(distinct: Boolean, tables: String!, columns: Array<String!>!, where: String!, groupBy: String!, having: String!, orderBy: String!, limit: String!)

Build an SQL query string from the given clauses.

open String!
buildUnionQuery(subQueries: Array<String!>!, sortOrder: String!, limit: String!)

Given a set of subqueries, all of which are SELECT statements, construct a query that returns the union of what those subqueries return.

open String!
buildUnionSubQuery(typeDiscriminatorColumn: String!, unionColumns: Array<String!>!, columnsPresentInTable: MutableSet<String!>!, computedColumnsOffset: Int, typeDiscriminatorValue: String!, selection: String!, groupBy: String!, having: String!)

Construct a SELECT statement suitable for use in a group of SELECT statements that will be joined through UNION operators in buildUnionQuery.

open String!
buildUnionSubQuery(typeDiscriminatorColumn: String!, unionColumns: Array<String!>!, columnsPresentInTable: MutableSet<String!>!, computedColumnsOffset: Int, typeDiscriminatorValue: String!, selection: String!, selectionArgs: Array<String!>!, groupBy: String!, having: String!)

open Int
delete(db: SQLiteDatabase, selection: String?, selectionArgs: Array<String!>?)

Perform a delete by combining all current settings and the information passed into this method.

open SQLiteDatabase.CursorFactory?

Gets the cursor factory to be used for the query, as last configured by setCursorFactory(android.database.sqlite.SQLiteDatabase.CursorFactory).

open MutableCollection<Pattern!>?

Gets the projection greylist for the query, as last configured by setProjectionGreylist.

open MutableMap<String!, String!>?

Gets the projection map for the query, as last configured by setProjectionMap(Map).

open String?

Returns the list of tables being queried

open Long

Perform an insert by combining all current settings and the information passed into this method.

open Boolean

Get if the query is marked as DISTINCT, as last configured by setDistinct(boolean).

open Boolean

Get if the query is marked as strict, as last configured by setStrict(boolean).

open Boolean

Get if the query is marked as strict, as last configured by setStrictColumns(boolean).

open Boolean

Get if the query is marked as strict, as last configured by setStrictGrammar(boolean).

open Cursor!
query(db: SQLiteDatabase!, projectionIn: Array<String!>!, selection: String!, selectionArgs: Array<String!>!, groupBy: String!, having: String!, sortOrder: String!)

Perform a query by combining all current settings and the information passed into this method.

open Cursor!
query(db: SQLiteDatabase!, projectionIn: Array<String!>!, selection: String!, selectionArgs: Array<String!>!, groupBy: String!, having: String!, sortOrder: String!, limit: String!)

Perform a query by combining all current settings and the information passed into this method.

open Cursor!
query(db: SQLiteDatabase!, projectionIn: Array<String!>!, selection: String!, selectionArgs: Array<String!>!, groupBy: String!, having: String!, sortOrder: String!, limit: String!, cancellationSignal: CancellationSignal!)

Perform a query by combining all current settings and the information passed into this method.

open Unit

Sets the cursor factory to be used for the query.

open Unit
setDistinct(distinct: Boolean)

Mark the query as DISTINCT.

open Unit

Sets a projection greylist of columns that will be allowed through, even when setStrict(boolean) is enabled.

open Unit

Sets the projection map for the query.

open Unit
setStrict(strict: Boolean)

When set, the selection is verified against malicious arguments.

open Unit
setStrictColumns(strictColumns: Boolean)

When enabled, verify that all projections and ContentValues only contain valid columns as defined by setProjectionMap(Map).

open Unit
setStrictGrammar(strictGrammar: Boolean)

When enabled, verify that all untrusted SQL conforms to a restricted SQL grammar.

open Unit
setTables(inTables: String?)

Sets the list of tables to query.

open Int
update(db: SQLiteDatabase, values: ContentValues, selection: String?, selectionArgs: Array<String!>?)

Perform an update by combining all current settings and the information passed into this method.

Public constructors

SQLiteQueryBuilder

Added in API level 1
SQLiteQueryBuilder()

Public methods

appendColumns

Added in API level 1
open static fun appendColumns(
    s: StringBuilder!,
    columns: Array<String!>!
): Unit

Add the names that are non-null in columns to s, separating them with commas.

appendWhere

Added in API level 1
open fun appendWhere(inWhere: CharSequence): Unit

Append a chunk to the WHERE clause of the query. All chunks appended are surrounded by parenthesis and ANDed with the selection passed to #query. The final WHERE clause looks like:

WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>)

Parameters
inWhere CharSequence: the chunk of text to append to the WHERE clause.
This value cannot be null.

appendWhereEscapeString

Added in API level 1
open fun appendWhereEscapeString(inWhere: String): Unit

Append a chunk to the WHERE clause of the query. All chunks appended are surrounded by parenthesis and ANDed with the selection passed to #query. The final WHERE clause looks like:

WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>)

Parameters
inWhere String: the chunk of text to append to the WHERE clause. it will be escaped to avoid SQL injection attacks.
This value cannot be null.

appendWhereStandalone

Added in API level 29
open fun appendWhereStandalone(inWhere: CharSequence): Unit

Add a standalone chunk to the WHERE clause of this query.

This method differs from appendWhere(CharSequence) in that it automatically appends AND to any existing WHERE clause already under construction before appending the given standalone expression wrapped in parentheses.

Parameters
inWhere CharSequence: the standalone expression to append to the WHERE clause. It will be wrapped in parentheses when it's appended.
This value cannot be null.

buildQuery

Added in API level 11
open fun buildQuery(
    projectionIn: Array<String!>!,
    selection: String!,
    groupBy: String!,
    having: String!,
    sortOrder: String!,
    limit: String!
): String!

Construct a SELECT statement suitable for use in a group of SELECT statements that will be joined through UNION operators in buildUnionQuery.

Parameters
projectionIn Array<String!>!: A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.
selection String!: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URL.
groupBy String!: A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.
having String!: A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.
sortOrder String!: How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
limit String!: Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Return
String! the resulting SQL SELECT statement

buildQuery

Added in API level 1
Deprecated in API level 15
open fun buildQuery(
    projectionIn: Array<String!>!,
    selection: String!,
    selectionArgs: Array<String!>!,
    groupBy: String!,
    having: String!,
    sortOrder: String!,
    limit: String!
): String!

Deprecated: This method's signature is misleading since no SQL parameter substitution is carried out. The selection arguments parameter does not get used at all. To avoid confusion, call buildQuery(String[],String,String,String,String,String) instead.

buildQueryString

Added in API level 1
open static fun buildQueryString(
    distinct: Boolean,
    tables: String!,
    columns: Array<String!>!,
    where: String!,
    groupBy: String!,
    having: String!,
    orderBy: String!,
    limit: String!
): String!

Build an SQL query string from the given clauses.

Parameters
distinct Boolean: true if you want each row to be unique, false otherwise.
tables String!: The table names to compile the query against.
columns Array<String!>!: A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.
where String!: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URL.
groupBy String!: A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.
having String!: A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.
orderBy String!: How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
limit String!: Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Return
String! the SQL query string

buildUnionQuery

Added in API level 1
open fun buildUnionQuery(
    subQueries: Array<String!>!,
    sortOrder: String!,