2012-12-02 130 views
3

如何檢查android數據庫中是否存在特定聯繫人以將其添加到數據庫中?如果檢查聯繫人姓名時可能存在其他聯繫人名稱,這樣任何一個可以幫助我,我的代碼是:如何檢查數據庫中是否存在聯繫人android

String where = ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY+" = ? AND "+ 
       ContactsContract.RawContacts.ACCOUNT_TYPE + " = ? AND "+ 
       ContactsContract.RawContacts.ACCOUNT_NAME+" = ?"; 
String[] whereArg = new String[] {displayName, AccountType, AccountName}; 

Cursor SameName = cResolver.query(ContactsContract.RawContacts.CONTENT_URI, null, where, whereArg,null); 

if (SameName == null || SameName.getCount() == 0) { 
        // the addContact method 
       }else { 

        // do nothing, the contact is exsist 

       } 
+0

啊哈好,謝謝@ WebnetMobile.com –

+0

只是想回答未回答的問題.... –

回答

0

其簡單的只是執行查詢說

String query ="SELECT * FROM " + TABLE_NAME + " WHERE displayName " + " = whateverNameYouWantToCheckFor" ; 

/*To avoid confusions of different contacts having the same name, extended the search criteria, by including other fields during the query.*/ 

Cursor c = cResolver.rawQuery(query,null); 

int duplicate = c.getCount(); // If there is a row containing the searched name then the count of the cursor will not be 0. 

if(duplicate !=0) 
{ 
    //Code to do whatever you want when the name exists already. 
} 
else 
{ 
    //Code to do whatever you want when the name does not exist. 
} 

希望它可以幫助...

相關問題