Android 游标查询计数
我正在开发一个小型应用程序,它从数据库读取数据并将其显示在一些自定义列表视图布局中。
我把所有的鸭子排成一排,但有一个问题我无法全神贯注,那就是在游标查询中执行计数
这是我的代码
private static final String fields[] = { "CarYear","CarMake","CarModel","CarInfo", BaseColumns._ID }; //BaseColumns._ID
和查询本身
Cursor data = database.query("Car060", fields, null, null, null, null,null);
的一个简单示例所以最大的问题是我怎样才能可以执行每个制造商的总计数,也可以仅执行总计数。
感谢您的任何帮助
最终使用 RawQuery
Cursor Count = database.rawQuery("SELECT COUNT(DISTINCT People) as NumberOfPeople FROM WhatEverDB", null);
I'm developing a small application that reads data from a database and displys it in a few custom listview layouts.
I got all my ducks in a row but have one issue where I can't wrap my mind around which is preforming a count within the cursor query
Here is a simple example of my code
private static final String fields[] = { "CarYear","CarMake","CarModel","CarInfo", BaseColumns._ID }; //BaseColumns._ID
And the query itself
Cursor data = database.query("Car060", fields, null, null, null, null,null);
So the big question is how can I either preform a total count per maker or just a total count.
Thanks for any assistance
Ended up using a RawQuery
Cursor Count = database.rawQuery("SELECT COUNT(DISTINCT People) as NumberOfPeople FROM WhatEverDB", null);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Cursor.getCount() 来获取游标返回的总行数。
要对各个“制造商”进行计数,您可以迭代游标并自己对它们进行计数,或者使用另一个查询向服务器发出 COUNT SQL 并从那里检索计数。
由于您已经获取此数据以在 ListView 中使用,我猜测前者更有效。
You can use Cursor.getCount() to get the total number of rows returned by the cursor.
To count individual 'makers' you can either iterate over the cursor, and count them yourself, or use another query to issue COUNT SQL to the server and retrieve the counts from there.
As you're already fetching this data for use in a ListView, I'd guess that the former is more efficient.
显而易见的解决方案是使用按制造商分组的查询。
The obvious solution would be to use a query that groups by maker.