2012-08-16 24 views
0

我測試SQL,它在SQLite的間諜工作正常:關於Android的ContentResolver的查詢組通過

select ifnull(_name, _number) as identifer, count(_id) as amount from call group by identifer 

而且我想在ContentConsolver使用它,但它也不能「按組」一起工作:

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

如果由'ContentResolver'查詢的'ContentProvider'不與組由暴露的URI或支持了一些URI參數組的參數你將不得不做Java中的分組代碼在讀完'Cursor'的全部內容之後。 – Jens 2012-08-16 05:51:01

+0

暫時解決,ICS出錯! add:String selection =「1 = 1)GROUP BY(identifer」; – thecr0w 2012-08-16 06:56:59

回答

0

@ njzk2通過使用HashSet:Group By in ContentResolver in Ice Cream Sandwich做了訣竅,但如果你想要sum,它不會與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(); 
}