ContentResolver.query方法的使用

来源:互联网 发布:淘宝假食品流通许可证 编辑:程序博客网 时间:2024/04/25 08:26

个人原创,转载请注明出处:http://blog.csdn.net/supluo/article/details/43954129

个人搞了个博客App,平时上个厕所,睡觉前等随便看两篇文章,总能有些收获,希望大家支持!http://blog.csdn.net/supluo/article/details/43489475


以下是文档上面的介绍:

public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Added in API level 1

Query the given URI, returning a Cursor over the result set.

For best performance, the caller should follow these guidelines:

  • Provide an explicit projection, to prevent reading data from storage that aren't going to be used.
  • Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.

Parameters
uriThe URI, using the content:// scheme, for the content to retrieve.projectionA list of which columns to return. Passing null will return all columns, which is inefficient.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 URI.selectionArgsYou may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.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 object, which is positioned before the first entry, or null


这个方法的使用主要是了解参数的含义,很多时候我们在使用时,后面的几个参数大多都传递的是null,其实好好了解下这些参数,能够更好的使用这些方法。上面的有几句英文提示和后面的参数使用是促使我记录这个方法使用的原因。

For best performance, the caller should follow these guidelines:

  • Provide an explicit projection, to prevent reading data from storage that aren't going to be used.
  • Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
这几句话的意思是说,为了更好的使用这个方法,调用者需要了解下面的两条知道原则:

1、提供明确的Projection参数来避免读取不会使用的数据字段。什么意思呢,举个例子,比如我们数据库存储了Persion这么一张记录表,有name,age等字段,如果要使用这个方法获取name字段,就明确指出第二个参数的值,这样就不用再去把age等这些不用的字段读取出来(读取数据库是需要消耗资源的);

2、在selection参数中使用诸如标记参数的形式比如'phone=?' 而不使用明确参数的形式,比如'phone=189xxx',这样做使得这些值会被视作为相同的缓存目的(caching purposes不知怎么翻译好),也就是说使用参数标记的形式是需要selectionArgs这个参数来支持的,而使用这种形式会使得selectionArgs会被作为缓存目的之类的东西来使用,这样查询效率应该更快一些。大致就是这个意思,这里描绘不当请见谅。

好的应用就是要注意这些细节地方,细节决定成败啊!


由于我们没有怎么使用过后面这些参数,因此这里简单举个例子进行对比使用

假如我们要实现: select * from myTable where var='const'

那么myTable 就是URI,

* 就是projection, 

selection 

    可以是 "var=?"那么selectionArgs就是 new String{“const”}

    也可以是“var = ‘const’”,那么selectionArgs就是null

    但是上面的指导原则说明了我们应该采用前者而不应该使用后者

sortOrder 最后一个参数就是排列顺序的意思。


下面贴上我之前遇到的使用这个问题的方法

从通讯录中查询电话号码(由于不同的手机号码存储格式不同,有些包含-,有些还包含空格)

      private string[] projections= new string[] {               Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Id,               Android.Provider.ContactsContract.CommonDataKinds.Phone.Number,               Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName            };      string[] to = new string[] { "%" + input.ToString().ToUpper() + "%" };      string where = "REPLACE(REPLACE(" + Android.Provider.ContactsContract.CommonDataKinds.Phone.Number + ",'-',''),' ','')" + " LIKE ?" + "OR UPPER(" + Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName + ") LIKE ?";      
_cursor0 = Application.Context.ContentResolver.Query(                            Android.Provider.ContactsContract.CommonDataKinds.Phone.ContentUri,                            projections,                            where,                            to, <span style="font-family: Arial, Helvetica, sans-serif;">null);</span>


个人搞了个博客App,平时上个厕所,睡觉前等随便看两篇文章,总能有些收获,希望大家支持!http://blog.csdn.net/supluo/article/details/43489475


1 0
原创粉丝点击