簡體   English   中英

將Distnict與getContentREsolver android Sqlite一起使用

[英]Use Distnict with getContentREsolver android Sqlite

我正在android中創建一個消息傳遞應用程序,我只想從唯一的聯系電話中獲取短信。

我嘗試了以下代碼,但無法正常工作。

String[] projection = new String[] { "_id","DISTINCT "+ContactsContract.PhoneLookup.NUMBER, "person", "body", "date", "type" };
Cursor cursor = getContentResolver().query(uri, projection,null, null, null);

它在這里拋出錯誤。

Caused by: android.database.sqlite.SQLiteException: near "DISTINCT": syntax error: , while compiling: SELECT _id, DISTINCT number, person, body, date, type FROM sms ORDER BY date DESC

有人可以幫我解決這個問題嗎?

您需要更改查詢。 DISTINCT關鍵字僅適用於所有行。 您需要的是查詢中的“分組依據”部分。

您可以將查詢更改為:

String[] projection = new String[] { "_id", ContactsContract.PhoneLookup.NUMBER, "person", "body", "date", "type" };
String selection = GROUP BY " + ContactsContract.PhoneLookup.NUMBER;
Cursor cursor = getContentResolver().query(uri, projection, selection, null, null);

我尚未對此進行測試,因此可能存在語法錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM