我测试sql,它在Sqlite Spy中工作正常:
select ifnull(_name, _number) as identifer, count(_id) as amount from call group by identifer
我想在ContentConsolver中使用它,但它无法与“group by”一起使用:
String[] projections = new String[] { "ifnull(name, number) as identifer", "count(_id) as amount" };
String group = "identifer";
//String selection = ") GROUP BY (" + group;
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, projections, null, null, null /*!group*/ );
我该怎么办?
答案 0 :(得分:0)
@ njzk2通过使用HashSet来实现这个技巧:Group By in ContentResolver in Ice Cream Sandwich,但是如果你想要总和,它就不能用于count()。 我认为最好的解决方案是制作CallLog数据库的副本,然后你可以使用rawQuery()或任何你想要的东西(可能是浪费性能)。
private void refreshDbCache()
{
CallDbCache dbCache = new CallDbCache(this);
dbCache.clear(CallDbCache.TB_CALL);
String[] columns = new String[] { CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME };
Cursor cursor = getContentResolver().query(URI_CALL, columns, null, null, null);
while (cursor != null && cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
dbCache.insert(CallDbCache.TB_CALL, new LogData(id, number, name, "", "", "", ""));
}
cursor.close();
dbCache.close();
}