Most visited

Recently visited

Added in API level 1

SQLiteQueryBuilder

public class SQLiteQueryBuilder
extends Object

java.lang.Object
   ↳ android.database.sqlite.SQLiteQueryBuilder


这是一个 SQLiteDatabase类,可帮助构建SQL查询以发送到 SQLiteDatabase对象。

Summary

Public constructors

SQLiteQueryBuilder()

Public methods

static void appendColumns(StringBuilder s, String[] columns)

将列中非空的名称添加到s中,用逗号分隔它们。

void appendWhere(CharSequence inWhere)

追加一个块到查询的WHERE子句。

void appendWhereEscapeString(String inWhere)

追加一个块到查询的WHERE子句。

String buildQuery(String[] projectionIn, String selection, String groupBy, String having, String sortOrder, String limit)

构建一个适用于一组SELECT语句的SELECT语句,它们将通过buildUnionQuery中的UNION运算符进行连接。

String buildQuery(String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)

此方法在API级别11中已弃用。此方法的签名具有误导性,因为未执行SQL参数替换。 选择参数参数根本不被使用。 为避免混淆,请拨打buildQuery(String[], String, String, String, String, String)

static String buildQueryString(boolean distinct, String tables, String[] columns, String where, String groupBy, String having, String orderBy, String limit)

从给定的子句构建一个SQL查询字符串。

String buildUnionQuery(String[] subQueries, String sortOrder, String limit)

给定一组子查询,所有这些都是SELECT语句,构造一个查询,返回这些子查询返回的联合。

String buildUnionSubQuery(String typeDiscriminatorColumn, String[] unionColumns, Set<String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, String[] selectionArgs, String groupBy, String having)

此方法在API级别11中已弃用。此方法的签名具有误导性,因为未执行SQL参数替换。 选择参数参数根本不被使用。 为避免混淆,请拨打buildUnionSubQuery(String, String[], Set , int, String, String, String, String)

String buildUnionSubQuery(String typeDiscriminatorColumn, String[] unionColumns, Set<String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, String groupBy, String having)

构建一个适用于一组SELECT语句的SELECT语句,它们将通过buildUnionQuery中的UNION运算符进行连接。

String getTables()

返回正在查询的表的列表

Cursor query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)

通过组合所有当前设置和传递给此方法的信息来执行查询。

Cursor query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)

通过组合所有当前设置和传递给此方法的信息来执行查询。

Cursor query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit, CancellationSignal cancellationSignal)

通过组合所有当前设置和传递给此方法的信息来执行查询。

void setCursorFactory(SQLiteDatabase.CursorFactory factory)

设置要用于查询的游标工厂。

void setDistinct(boolean distinct)

将查询标记为DISTINCT。

void setProjectionMap(Map<StringString> columnMap)

设置查询的投影图。

void setStrict(boolean flag)

设置后,选择将根据恶意参数进行验证。

void setTables(String inTables)

设置要查询的表的列表。

Inherited methods

From class java.lang.Object

Public constructors

SQLiteQueryBuilder

Added in API level 1
SQLiteQueryBuilder ()

Public methods

appendColumns

Added in API level 1
void appendColumns (StringBuilder s, 
                String[] columns)

将列中非空的名称添加到s中,用逗号分隔它们。

Parameters
s StringBuilder
columns String

appendWhere

Added in API level 1
void appendWhere (CharSequence inWhere)

追加一个块到查询的WHERE子句。 附加的所有块被括号括起来,并与传递给query(SQLiteDatabase, String[], String, String[], String, String, String)的选择进行与运算。 最后的WHERE子句如下所示:WHERE(<append chunk 1> <append chunk2>)AND(<query()selection parameter>)

Parameters
inWhere CharSequence: the chunk of text to append to the WHERE clause.

appendWhereEscapeString

Added in API level 1
void appendWhereEscapeString (String inWhere)

追加一个块到查询的WHERE子句。 附加的所有块都被括号括起来,并与传递给query(SQLiteDatabase, String[], String, String[], String, String, String)的选择进行与运算。 最后的WHERE子句如下所示: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

buildQuery

Added in API level 11
String buildQuery (String[] projectionIn, 
                String selection, 
                String groupBy, 
                String having, 
                String sortOrder, 
                String limit)

构建一个适用于一组SELECT语句的SELECT语句,它们将通过buildUnionQuery中的UNION运算符进行连接。

Parameters
projectionIn 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.
Returns
String the resulting SQL SELECT statement

buildQuery

Added in API level 1
String buildQuery (String[] projectionIn, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String sortOrder, 
                String limit)

此方法在API级别11中已弃用。
由于没有执行SQL参数替换,因此此方法的签名具有误导性。 选择参数参数根本不被使用。 为避免混淆,请拨打buildQuery(String[], String, String, String, String, String)

Parameters
projectionIn String
selection String
selectionArgs String
groupBy String
having String
sortOrder String
limit String
Returns
String

buildQueryString

Added in API level 1
String buildQueryString (boolean distinct, 
                String tables, 
                String[] columns, 
                String where, 
                String groupBy, 
                String having, 
                String orderBy, 
                String limit)

从给定的子句构建一个SQL查询字符串。

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 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.
Returns
String the SQL query string

buildUnionQuery

Added in API level 1
String buildUnionQuery (String[] subQueries, 
                String sortOrder, 
                String limit)

给定一组子查询,所有这些都是SELECT语句,构造一个查询,返回这些子查询返回的联合。

Parameters
subQueries String: an array of SQL SELECT statements, all of which must have the same columns as the same positions in their results
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: The limit clause, which applies to the entire union result set
Returns
String the resulting SQL SELECT statement

buildUnionSubQuery

Added in API level 1
String buildUnionSubQuery (String typeDiscriminatorColumn, 
                String[] unionColumns, 
                Set<String> columnsPresentInTable, 
                int computedColumnsOffset, 
                String typeDiscriminatorValue, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having)

此方法在API级别11中已弃用。
由于没有执行SQL参数替换,因此此方法的签名具有误导性。 选择参数参数根本不被使用。 为避免混淆,请拨打buildUnionSubQuery(String, String[], Set , int, String, String, String, String)

Parameters
typeDiscriminatorColumn String
unionColumns String
columnsPresentInTable Set
computedColumnsOffset int
typeDiscriminatorValue String
selection String
selectionArgs String
groupBy String
having String
Returns
String

buildUnionSubQuery

Added in API level 11
String buildUnionSubQuery (String typeDiscriminatorColumn, 
                String[] unionColumns, 
                Set<String> columnsPresentInTable, 
                int computedColumnsOffset, 
                String typeDiscriminatorValue, 
                String selection, 
                String groupBy, 
                String having)

构建一个适用于一组SELECT语句的SELECT语句,它们将通过buildUnionQuery中的UNION运算符进行连接。

Parameters
typeDiscriminatorColumn String: the name of the result column whose cells will contain the name of the table from which each row was drawn.
unionColumns String: the names of the columns to appear in the result. This may include columns that do not appear in the table this SELECT is querying (i.e. mTables), but that do appear in one of the other tables in the UNION query that we are constructing.
columnsPresentInTable Set: a Set of the names of the columns that appear in this table (i.e. in the table whose name is mTables). Since columns in unionColumns include columns that appear only in other tables, we use this array to distinguish which ones actually are present. Other columns will have NULL values for results from this subquery.
computedColumnsOffset int: all columns in unionColumns before this index are included under the assumption that they're computed and therefore won't appear in columnsPresentInTable, e.g. "date * 1000 as normalized_date"
typeDiscriminatorValue String: the value used for the type-discriminator column in this subquery
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.
Returns
String the resulting SQL SELECT statement

getTables

Added in API level 1
String getTables ()

返回正在查询的表的列表

Returns
String the list of tables being queried

query

Added in API level 1
Cursor query (SQLiteDatabase db, 
                String[] projectionIn, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String sortOrder)

通过组合所有当前设置和传递给此方法的信息来执行查询。

Parameters
db SQLiteDatabase: the database to query on
projectionIn 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.
selectionArgs String: You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
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.
Returns
Cursor a cursor over the result set

也可以看看:

query

Added in API level 1
Cursor query (SQLiteDatabase db, 
                String[] projectionIn, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String sortOrder, 
                String limit)

通过组合所有当前设置和传递给此方法的信息来执行查询。

Parameters
db SQLiteDatabase: the database to query on
projectionIn 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.
selectionArgs String: You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
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.
Returns
Cursor a cursor over the result set

也可以看看:

query

Added in API level 16
Cursor query (SQLiteDatabase db, 
                String[] projectionIn, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String sortOrder, 
                String limit, 
                CancellationSignal cancellationSignal)

通过组合所有当前设置和传递给此方法的信息来执行查询。

Parameters
db SQLiteDatabase: the database to query on
projectionIn 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.
selectionArgs String: You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
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.
cancellationSignal CancellationSignal: A signal to cancel the operation in progress, or null if none. If the operation is canceled, then OperationCanceledException will be thrown when the query is executed.
Returns
Cursor a cursor over the result set

也可以看看:

setCursorFactory

Added in API level 1
void setCursorFactory (SQLiteDatabase.CursorFactory factory)

设置要用于查询的游标工厂。 对于数据库上的所有查询,您可以使用一个工厂,但在执行此查询时指定工厂通常更容易。

Parameters
factory SQLiteDatabase.CursorFactory: the factory to use.

setDistinct

Added in API level 1
void setDistinct (boolean distinct)

将查询标记为DISTINCT。

Parameters
distinct boolean: if true the query is DISTINCT, otherwise it isn't

setProjectionMap

Added in API level 1
void setProjectionMap (Map<StringString> columnMap)

设置查询的投影图。 投影映射从调用者传递到查询中的列名映射到数据库列名。 这对重命名列以及在进行连接时消除歧义列名非常有用。 例如,您可以将“name”映射到“people.name”。 如果设置投影映射,则必须包含用户可能请求的所有列名称,即使键和值相同。

Parameters
columnMap Map: maps from the user column names to the database column names

setStrict

Added in API level 14
void setStrict (boolean flag)

设置后,选择将根据恶意参数进行验证。 使用此类创建使用buildQueryString(boolean, String, String[], String, String, String, String, String)的语句时,非数字限制会引发异常。 如果指定了投影映射,那么不在该映射中的字段将被忽略。 如果此类用于直接使用query(SQLiteDatabase, String[], String, String[], String, String, String)query(SQLiteDatabase, String[], String, String[], String, String, String, String)执行语句,另外还会捕获括号转义选择。 总结:为了最大限度地保护恶意第三方应用程序(例如内容提供商使用者),请确保执行以下操作:

  • Set this value to true
  • Use a projection map
  • Use one of the query overloads instead of getting the statement as a sql string
By default, this value is false.

Parameters
flag boolean

setTables

Added in API level 1
void setTables (String inTables)

设置要查询的表的列表。 可以指定多个表来执行连接。 例如:setTables(“foo,bar”)setTables(“foo LEFT OUTER JOIN bar ON(foo.id = bar.foo_id)”)

Parameters
inTables String: the list of tables to query on

Hooray!