您的位置:首页 > 产品设计 > UI/UE

ContentResolver.query()的参数 说明

2013-04-01 15:23 302 查看


public final Cursorquery (Uri uri,String[] projection, String selection, String[] selectionArgs,StringsortOrder)

Since: API Level 1

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

For best performance, the caller should follow theseguidelines:

Provide an explicit projection, to prevent reading data fromstorage that aren't going to be used.
Use question mark parameter markers such as 'phone=?' insteadof explicit values in the
selection
parameter, so thatqueries that differ only by those values will be recognized as thesame for caching purposes.

Parameters
uriThe URI, using the content:// scheme, for the content toretrieve.
projectionA list of which columns to return. Passing null will return allcolumns, which is inefficient.
selectionA filter declaring which rows to return, formatted as an SQLWHERE clause (excluding the WHERE itself). Passing null will returnall rows for the given URI.
selectionArgsYou may include ?s in selection, which will be replaced by thevalues from selectionArgs, in the order that they appear in theselection. 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 defaultsort order, which may be unordered.
Returns
A Cursor object, which is positioned before the first entry, ornull

解释一下:假如一条sql语句如下:

select * from anyTable where var='const'

那么anyTable就是uri,*就是projection,selection是“var=?",selectionArgs写成这样:newString[]{'const‘}

至于最后一个就简单了,就是排序方式。

其实 第三个参数是sql语句where部分,如果第三个参数不带 “?”,那么第四个参数 写成 null;

..........................................................................................//华丽的分割线...................................................................

from:http://blog.csdn.net/ada168855/article/details/8475993
public Cursor query(String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit);
参数说明:
table:数据库表的名称
columns:数据库列名称数组 写入后最后返回的Cursor中只能查到这里的列的内容

selection:查询条件 

selectionArgs:查询结果 
groupBy:分组列
having:分组条件
orderBy:排序列
limit:分页查询限制
Cursor:返回值,将查询到的结果都存在Cursor
Cursor是一个游标接口,每次查询的结果都会保存在Cursor中 可以通过遍历Cursor的方法拿到当前查询到的所有信息。
Cursor的方法
moveToFirst() //将Curor的游标移动到第一条

moveToLast()///将Curor的游标移动到最后一条

move(int offset)//将Curor的游标移动到指定ID

moveToNext()//将Curor的游标移动到下一条

moveToPrevious()//将Curor的游标移动到上一条

getCount() //得到Cursor 总记录条数

isFirst() //判断当前游标是否为第一条记录

isLast()//判断当前游标是否为最后一条数据

getInt(int columnIndex)    //根据列名称获得列索引ID

getString(int columnIndex)//根据索引ID 拿到表中存的字段


这里给出一个例子遍历Cursor的例子
private
void query(SQLiteDatabase db) {
// 把整张表的所有数据query到cursor中

Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
//判断cursor不为空 这个很重要

if (cursor != null) {
// 循环遍历cursor

while (cursor.moveToNext()) {
// 拿到每一行name 与hp的数值

String name = cursor.getString(cursor.getColumnIndex("name"));
String hp = cursor.getString(cursor.getColumnIndex("hp"));
Log.v("info", "姓名是 " + name + "hp为 " + hp);
}
// 关闭

cursor.close();
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: