查询数据库以获取列表,例如给定名称、电子邮件或 phone

Query db for getting list either like given name, email or phone

我想获取联系人列表

1. LIKE given text display_name  **OR**

2. LIKE given text email   **OR**

3. LIKE given text phone no

以下是我的查询

String text = "testing";
    ContentResolver cr = getContentResolver();

    String[] mSelectionArgs = {  "%" + text + "%" };

    String SELECTION =
            (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                    ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?" :
                    ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?")
            + " OR  " +ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?"
                    + " OR  " + ContactsContract.CommonDataKinds.Email.DATA+ " LIKE ?"
            ;

    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            SELECTION, mSelectionArgs, null);

android.database.sqlite.SQLiteException: no such column: data1 (code 1): , while compiling: SELECT times_contacted, contacts_status_updates.status AS contact_status, phonetic_name, phonetic_name_style, link_accounts AS link, is_user_profile, lookup, phonebook_label_alt, contacts_status_updates.status_icon AS contact_status_icon, last_time_contacted, contact_last_updated_timestamp, sec_custom_vibration, _id, display_name_source, photo_uri, photo_thumb_uri, agg_presence.chat_capability AS contact_chat_capability, photo_id, send_to_voicemail, display_name_alt AS display_name_reverse, custom_ringtone, name_raw_contact_id, photo_file_id, has_phone_number, contacts_status_updates.status_label AS contact_status_label, link_type1, phonebook_bucket, display_name, phonebook_bucket_alt, has_email, sort_key_alt, phonebook_label, dirty_contact, in_visible_group, starred, link_count, display_name_alt, sort_key, agg_presence.mode AS contact_presence, sec_custom_alert, is_private, contacts_status_updates.status_res_package AS contact_status_res_package, contacts_status_updates.status_ts AS contact_status_ts FROM view_contacts_restricted LEFT OUTER JOIN agg_presence ON (view_contacts_restricted._id = agg_presence.presence_contact_id) LEFT OUTER JOIN status_updates contacts_status_updates ON (status_update_id=contacts_status_updates.status_update_data_id) WHERE ((1)) AND (((display_name LIKE ?) OR  (data1 LIKE ?) OR  (data1 LIKE ? )))
                                                                                      at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
                                                                                      at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
                                                                                      at android.content.ContentProviderProxy.query(ContentProviderNative.java:385)
                                                                                      at android.content.ContentResolver.query(ContentResolver.java:417)
                                                                                      at android.content.ContentResolver.query(ContentResolver.java:360)
                                                                                      at com.icpl.dhrupalpatel.searchconatact.MainActivity.getDetail(MainActivity.java:81)
                                                                                      at com.icpl.dhrupalpatel.searchconatact.MainActivity.onClick(MainActivity.java:49)
                                                                                      at android.view.View.performClick(View.java:4475)
                                                                                      at android.view.View$PerformClick.run(View.java:18796)
                                                                                      at android.os.Handler.handleCallback(Handler.java:730)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                      at android.os.Looper.loop(Looper.java:137)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5455)
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                                                                                      at dalvik.system.NativeStart.main(Native Method)

如您在错误中所见,table 中不存在 data1

您的查询包含此语句:OR (data1 LIKE ?) OR (data1 LIKE ? )。可能这是一个错误,但无论如何都要检查一下。

有两种可能:

  1. 检查这个 data1 的位置并使用 TABLENAME.data1 LIKE ...
  2. data1 在任何 table 中都不存在,这是一个错误。在这种情况下,通过编辑数据库将其添加到 table 中。

检查此列是否存在并对所有 where 条件使用 tablename.field。

编辑

查看有关如何从 android 台设备 here

检索联系人列表的官方指南

注意:阅读所有 POST 而不仅仅是第一行

希望这对您有所帮助

您可以使用 ContactsContract.Contacts.CONTENT_FILTER_URI 来完成。 This 是 link。