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)
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
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
- ContentResolver.query方法的使用
- ContentResolver.query()的用法
- ContentResolver query方法 参数详解
- ContentResolver的四个常用方法:delete().query(),update(),insert()
- ContentResolver.query()的四个参数
- ContentResolver.query()的参数 说明
- ContentResolver.query()的参数 说明
- ContentResolver query()的四个参数
- ContentResolver.query()的四个参数
- ContentResolver().query的分组查询
- ContentResolver query
- Android中ContentResolver的query方法中参数的详细解析
- ContentResolver.update以及query方法参数详细说明
- ContentResolver query 参数详解
- ContentResolver query 参数详解
- ContentResolver query 参数详解
- ContentResolver query 参数详解
- ContentResolver query 参数详解
- java集合框架的体系结构详细说明
- 接口和抽象类有什么区别
- JS加载性能优化
- IOS多线程之GCD
- JQuery+ajax+jsonp 跨域访问
- ContentResolver.query方法的使用
- STL学习小结
- oc中类的继承
- IOS多线程之GCD(2)
- poj 2418 Hardwood Species
- DEVICE_ATTR的使用
- UVa 128 Software CRC(模运算)
- 备库log_archive_dest_2的问题
- android 手机上设置呼叫转移