2015-09-20 60 views
2

我正在android中創建一個消息應用程序,我只想從唯一的聯繫人號碼獲取短信。使用Distnict和getContentREsolver android Sqlite

我試過下面的代碼,但它不工作。

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 

有人可以爲我解開這個。

+0

請參閱[this](https://www.sqlite.org/lang_select.html) – pskink

+0

Android版本的最低要求是多少? –

回答

0

您需要更改查詢。 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); 

我沒有測試此還,所以有可能是語法錯誤。

+0

它不起作用。顯示相同類型的錯誤 –

+0

讓我測試它.. – Rachit

相關問題