简体   繁体   English

Android:ContentResolver查询中的无效列数(*)

[英]Android: Invalid column count(*) in ContentResolver query()

I am developing an Android application and I want to read the call log. 我正在开发一个Android应用程序,我想阅读通话记录。 I have found tons of ways to do it but I want to use more complex queries such as COUNT, SUM combined with GROUP BY. 我找到了很多方法,但是我想使用更复杂的查询,例如COUNT,SUM和GROUP BY。

The following works: 以下作品:

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor cursor = context.getContentResolver().query(allCalls, null, null, null, null);

If I want to use more advanced syntax, like the following 如果我想使用更高级的语法,如下所示

Uri allCalls = Uri.parse("content://call_log/calls");
String[] columns = new String[] { CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, "count(" + CallLog.Calls.NUMBER + ") as total" };
String selection = " 0 == 0) (GROUP BY (" + CallLog.Calls.NUMBER + "), (" + CallLog.Calls.CACHED_NAME + ")";
Cursor cursor = context.getContentResolver().query(allCalls, columns, selection, null, "count("+ CallLog.Calls.NUMBER +") DESC");

the application crashes with the following error: 该应用程序崩溃并显示以下错误:

java.lang.IllegalArgumentException: Invalid column count(number) as total

I have tried different variants like count(*) as total, count(*) without the "as total", but still nothing. 我尝试了不同的变体,例如count(*)作为总数,count(*)没有“ as total”,但仍然没有。

Do you have any ideas what am I doing wrong? 你有什么主意我在做什么错吗?

I am developing in Android API level 19 (4.4.2) 我正在使用Android API级别19(4.4.2)开发

Thanks 谢谢

It is crashing because your use of count(" + CallLog.Calls.NUMBER + ") as total in the columns variable is illegal. 之所以崩溃,是因为您在columns变量中使用count(" + CallLog.Calls.NUMBER + ") as total是非法的。 You can only put Strings representing column names into that argument to contentResolver.query() , such as CallLog.Calls.NUMBER, nothing else. 您只能将表示列名的字符串放入contentResolver.query()参数中,例如CallLog.Calls.NUMBER,仅此而已。 You can retrieve a count by running a query and then getting the count from the cursor returned, but the content provider/resolver model really does not support a group by clause. 您可以通过运行查询来获取计数,然后从返回的游标中获取计数,但是内容提供者/解析器模型确实不支持group by子句。 I've never tried but maybe someone else knows a workaround for that. 我从未尝试过,但也许其他人知道解决办法。 But your group by clause is being interpreted as part of an SQL where clause. 但是您的group by子句被解释为SQL where子句的一部分。 It does support an order by clause so replacing your query call with the following may be helpful : 它确实支持order by子句,因此将query调用替换为以下内容可能会有所帮助:

Cursor cursor = context.getContentResolver().query(allCalls, columns, selection, null, CallLog.Calls.NUMBER + "," + CallLog.Calls.CACHED_NAME);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM