以下是文档上面的介绍:
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](http://www.android-doc.com/reference/android/database/Cursor.html) 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
selectionparameter, 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
selectionparameter, 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’
1 、那么myTable 就是URI,
2、* 就是projection,
3、selection可以是 “var=?”
4、那么selectionArgs就是 new String{“const”} 也可以是“var = ‘const’”,那么selectionArgs就是null 但是上面的指导原则说明了我们应该采用前者而不应该使用后者
5、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>
val cursor: Cursor? = contentResolver.query(
data.data!!,
projection,
null,null,null
)

本文介绍了Android中使用Cursor查询数据的最佳实践,包括如何通过指定projection避免读取不必要的数据字段,以及如何利用参数标记提高查询效率。

3048

被折叠的 条评论
为什么被折叠?



