
上QQ阅读APP看书,第一时间看更新
5.2.5 查询数据
比起之前的添加、删除和修改操作来说,查询数据可能会复杂一些,但是一旦理解,也很好操作,实例代码如下:

上面是查询表pingred中所有的数据,也可以直接查询想要查询的数据:


由上面两个用法的例子可以知道查询方法最短的重载方法也有7个参数:query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy),其实并不复杂,它们的每个参数都是对应SQL语句查询的格式:
●table:表名;
●columns:列名;
●selection:约束条件(where);
●selectionArgs:约束条件的值;
●groupBy:需要分组的列;
●having:groupBy后进一步约束条件;
●orderBy:对查询到的数据进行排序。
通过遍历Cursor对象能获取想要查询的数据,而Cursor也提供了很多方法供开发者使用,如图5.2所示。

图5.2 Cursor提供的方法