1

我测试了 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*/ );

我该怎么办?

4

1 回答 1

0

@njzk2通过在 Ice Cream Sandwich 的 ContentResolver 中使用 HashSet: Group By 来做到这一点,但如果你想要总和,它不适用于 count()。我认为最好的解决方案是制作 CallLog Database 的副本,然后您可以使用 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();
}
于 2012-08-20T07:36:44.293 回答