【Content Provider】SQLiteQueryBuilder类

http://hi.baidu.com/bvtfbfhvaempsve/item/696ee05696e7433933e0a916

SQLiteDatabase db = mOpenHelper.getReadableDatabase(),得到一个可读的SQLiteDatabase 实例。
Cursor c = qb.query(db, projection, selection, selectionArgs, null,null, orderBy)语句,这个查询类似于一个标准的SQL查询,但是这个查询是SQLiteQueryBuilder 来发起的,而不是SQLiteDatabase 直接发起的,所以在参数方面略有不同。这个函数为 query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)下边将各个参数介绍一下。
第一个参数为要查询的数据库实例。
第二个参数是一个字符串数组,里边的每一项代表了需要返回的列名。
第三个参数相当于SQL语句中的where部分。
第四个参数是一个字符串数组,里边的每一项依次替代在第三个参数中出现的问号(?)。
第五个参数相当于SQL语句当中的groupby部分。
第六个参数相当于SQL语句当中的having部分。
第七个参数描述是怎么进行排序。
第八个参数相当于SQL当中的limit部分,控制返回的数据的个数。


http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html



public class

SQLiteQueryBuilder

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

Class Overview


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

Summary


Public Constructors
SQLiteQueryBuilder()
Public Methods
static void appendColumns( StringBuilder s, String[] columns)
Add the names that are non-null in columns to s, separating them with commas.
void appendWhere( CharSequence inWhere)
Append a chunk to the WHERE clause of the query.
void appendWhereEscapeString( String inWhere)
Append a chunk to the WHERE clause of the query.
String buildQuery( String[] projectionIn, String selection, String groupBy, String having, String sortOrder, String limit)
Construct a SELECT statement suitable for use in a group of SELECT statements that will be joined through UNION operators in buildUnionQuery.
String buildQuery( String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)
This method was deprecated in API level 11. 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.
static String buildQueryString(boolean distinct, String tables, String[] columns, String where, String groupBy, String having, String orderBy, String limit)
Build an SQL query string from the given clauses.
String buildUnionQuery( String[] subQueries, String sortOrder, String limit)
Given a set of subqueries, all of which are SELECT statements, construct a query that returns the union of what those subqueries return.
String buildUnionSubQuery( String typeDiscriminatorColumn, String[] unionColumns, Set< String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, String[] selectionArgs, String groupBy, String having)
This method was deprecated in API level 11. 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 buildUnionSubQuery(String, String[], Set, int, String, String, String, String) instead.
String buildUnionSubQuery( String typeDiscriminatorColumn, String[] unionColumns, Set< String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, String groupBy, String having)
Construct a SELECT statement suitable for use in a group of SELECT statements that will be joined through UNION operators in buildUnionQuery.
String getTables()
Returns the list of tables being queried
Cursor query( SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)
Perform a query by combining all current settings and the information passed into this method.
Cursor query( SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)
Perform a query by combining all current settings and the information passed into this method.
Cursor query( SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit, CancellationSignal cancellationSignal)
Perform a query by combining all current settings and the information passed into this method.
void setCursorFactory( SQLiteDatabase.CursorFactory factory)
Sets the cursor factory to be used for the query.
void setDistinct(boolean distinct)
Mark the query as DISTINCT.
void setProjectionMap( Map< StringString> columnMap)
Sets the projection map for the query.
void setStrict(boolean flag)
When set, the selection is verified against malicious arguments.
void setTables( String inTables)
Sets the list of tables to query.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors


public SQLiteQueryBuilder ()
Added in API level 1

Public Methods


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

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

public void appendWhere (CharSequence inWhere)
Added in API level 1

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(SQLiteDatabase, String[], String, String[], String, String, String). The final WHERE clause looks like: WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>)

Parameters
inWherethe chunk of text to append to the WHERE clause.
public void appendWhereEscapeString (String inWhere)
Added in API level 1

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(SQLiteDatabase, String[], String, String[], String, String, String). The final WHERE clause looks like: WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>)

Parameters
inWherethe chunk of text to append to the WHERE clause. it will be escaped to avoid SQL injection attacks
public String buildQuery (String[] projectionIn, String selection, String groupBy, String having, String sortOrder, String limit)
Added in API level 11

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

Parameters
projectionInA 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.
selectionA 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.
groupByA 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.
havingA 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.
sortOrderHow 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.
limitLimits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns
  • the resulting SQL SELECT statement
public String buildQuery (String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)
Added in API level 1

This method was deprecated in API level 11.
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.

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

Build an SQL query string from the given clauses.

Parameters
distincttrue if you want each row to be unique, false otherwise.
tablesThe table names to compile the query against.
columnsA 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.
whereA 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.
groupByA 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.
havingA 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.
orderByHow 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.
limitLimits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns
  • the SQL query string
public String buildUnionQuery (String[] subQueries, String sortOrder, String limit)
Added in API level 1

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

Parameters
subQueriesan array of SQL SELECT statements, all of which must have the same columns as the same positions in their results
sortOrderHow 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.
limitThe limit clause, which applies to the entire union result set
Returns
  • the resulting SQL SELECT statement
public String buildUnionSubQuery (String typeDiscriminatorColumn, String[] unionColumns, Set<String> columnsPresentInTable, int computedColumnsOffset, String typeDiscriminatorValue, String selection, String[] selectionArgs, String groupBy, String having)
Added in API level 1

This method was deprecated in API level 11.
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 buildUnionSubQuery(String, String[], Set, int, String, String, String, String) instead.

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

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

Parameters
typeDiscriminatorColumnthe name of the result column whose cells will contain the name of the table from which each row was drawn.
unionColumnsthe 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.
columnsPresentInTablea 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.
computedColumnsOffsetall 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"
typeDiscriminatorValuethe value used for the type-discriminator column in this subquery
selectionA 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.
groupByA 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.
havingA 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
  • the resulting SQL SELECT statement
public String getTables ()
Added in API level 1

Returns the list of tables being queried

Returns
  • the list of tables being queried
public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)
Added in API level 1

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

Parameters
dbthe database to query on
projectionInA 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.
selectionA 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.
selectionArgsYou 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.
groupByA 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.
havingA 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.
sortOrderHow 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.
limitLimits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns
  • a cursor over the result set
public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)
Added in API level 1

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

Parameters
dbthe database to query on
projectionInA 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.
selectionA 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.
selectionArgsYou 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.
groupByA 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.
havingA 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.
sortOrderHow 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
  • a cursor over the result set
public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit, CancellationSignal cancellationSignal)
Added in API level 16

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

Parameters
dbthe database to query on
projectionInA 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.
selectionA 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.
selectionArgsYou 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.
groupByA 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.
havingA 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.
sortOrderHow 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.
limitLimits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
cancellationSignalA 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
  • a cursor over the result set
public void setCursorFactory (SQLiteDatabase.CursorFactory factory)
Added in API level 1

Sets the cursor factory to be used for the query. You can use one factory for all queries on a database but it is normally easier to specify the factory when doing this query.

Parameters
factorythe factory to use.
public void setDistinct (boolean distinct)
Added in API level 1

Mark the query as DISTINCT.

Parameters
distinctif true the query is DISTINCT, otherwise it isn't
public void setProjectionMap (Map<StringString> columnMap)
Added in API level 1

Sets the projection map for the query. The projection map maps from column names that the caller passes into query to database column names. This is useful for renaming columns as well as disambiguating column names when doing joins. For example you could map "name" to "people.name". If a projection map is set it must contain all column names the user may request, even if the key and value are the same.

Parameters
columnMapmaps from the user column names to the database column names
public void setStrict (boolean flag)
Added in API level 14

When set, the selection is verified against malicious arguments. When using this class to create a statement using buildQueryString(boolean, String, String[], String, String, String, String, String), non-numeric limits will raise an exception. If a projection map is specified, fields not in that map will be ignored. If this class is used to execute the statement directly using query(SQLiteDatabase, String[], String, String[], String, String, String) or query(SQLiteDatabase, String[], String, String[], String, String, String, String), additionally also parenthesis escaping selection are caught. To summarize: To get maximum protection against malicious third party apps (for example content provider consumers), make sure to do the following:

  • 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.
public void setTables (String inTables)
Added in API level 1

Sets the list of tables to query. Multiple tables can be specified to perform a join. For example: setTables("foo, bar") setTables("foo LEFT OUTER JOIN bar ON (foo.id = bar.foo_id)")

Parameters
inTablesthe list of tables to query on

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值