从ContactsContract表获取数据时获取SQLITE异常

时间:2014-02-07 10:20:11

标签: java android sqlite contactscontract

我正在尝试从ContactsConstract表中通过查询中的ContentProvider获取所有联系人并将其保存到我自己的SQLite数据库中,但是在获取此内容时遇到了SQLiteException。 刚开始时我刚刚写了一个查询来获取联系人的数据 Plz帮助我并建议我如何获取数据并存储到我自己的数据库中。

这是我的源代码:

public class WebpreneurActivity extends ListActivity {

            private static final int CONTACT_CREATE = 0;
            private static final int CONTACT_EDIT = 1;
         public static  long id1;
            //select the second one, Android view menu
            private static final int INSERT_ID = Menu.FIRST;
            private static final int DELETE_ID = Menu.FIRST + 1;

            private DBHandler dbHelper;

            private Cursor c;
            ImageButton imageButton;
             public static long  rowId;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                Log.d("database1" ,"0");
                Log.d("Your Location4", "ok4:");
                super.onCreate(savedInstanceState);
                Log.d("database1" ,"1");
                setContentView(R.layout.activity_webpreneur);
                Log.d("database1" ,"2");
                //dbHelper = new DBHandler(this);
                Log.d("database1" ,"3");
            //  dbHelper.open();
                 **ContentResolver cr = getContentResolver();
                  c = cr.query(
                            Data.CONTENT_URI, 
                            null, 
                            Data.HAS_PHONE_NUMBER + "!=0 AND (" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?)", 
                            new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
                            Data.CONTACT_ID);
                    while (c.moveToNext()) {
                        long id = c.getLong(c.getColumnIndex(Data.CONTACT_ID));
                        String name = c.getString(c.getColumnIndex(Data.DISPLAY_NAME));
                        String data1 = c.getString(c.getColumnIndex(Data.DATA1));
                        System.out.println(id + ", name=" + name + ", data1=" + data1);
                    }**
            }

我的logcat在这里:

02-07 15:08:32.827: D/database1(16568): 0
02-07 15:08:32.827: D/Your Location4(16568): ok4:
02-07 15:08:32.837: D/database1(16568): 1
02-07 15:08:32.977: D/database1(16568): 2
02-07 15:08:32.977: D/database1(16568): 3
02-07 15:08:33.027: D/AndroidRuntime(16568): Shutting down VM
02-07 15:08:33.027: W/dalvikvm(16568): threadid=1: thread exiting with uncaught exception (group=0x4001d578)
02-07 15:08:33.067: E/AndroidRuntime(16568): FATAL EXCEPTION: main
02-07 15:08:33.067: E/AndroidRuntime(16568): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity}: android.database.sqlite.SQLiteException: no such column: has_phone_number: , while compiling: SELECT data_version, phonetic_name, contact_id, phonetic_name_style, lookup, data12, data11, mimetype, data10, data15, data14, data13, data_sync1, display_name_source, data_sync3, data_sync2, agg_presence.chat_capability AS contact_chat_capability, data_sync4, account_type, custom_ringtone, data1, status_updates.status AS status, data4, data5, data2, data3, data8, data9, data6, group_sourceid, data7, account_name, display_name, in_visible_group, display_name_alt, is_primary, contacts_status_updates.status_res_package AS contact_status_res_package, contacts_status_updates.status_ts AS contact_status_ts, raw_contact_id, times_contacted, contacts_status_updates.status AS contact_status, status_updates.status_res_package AS status_res_package, status_updates.status_icon AS status_icon, contacts_status_updates.status_icon AS contact_status_icon, version, presence.mode AS mode, last_time_contacted, _id, res_package, name_verified, is_super_primary, dirty, status_updates.status_ts AS status_ts, photo_id, send_to_voicemail, name_raw_contact_id, contacts_status_updates.status_label AS contact_status_label, status_updates.status_label AS status_label, sort_key_alt, starred, sort_key, sourceid, agg_presence.mode AS contact_presence FROM ( SELECT * FROM view_data_restricted WHERE (mimetype IS NULL OR mimetype NOT IN ('vnd.android.cursor.item/vnd.sonyericsson.android.hrphoto')) ) data LEFT OUTER JOIN agg_presence ON (agg_presence.presence_contact_id=contact_id) LEFT OUTER JOIN status_updates contacts_status_updates ON (status_update_id=contacts_status_updates.status_update_data_id) LEFT OUTER JOIN presence ON (presence_data_id=data._id) LEFT OUTER JOIN status_updates ON (status_updates.status_update_data_id=data._id) WHERE (1) AND (has_phone_number!=0 AND (mimetype=? OR mimetype=?)) ORDER BY contact_id
02-07 15:08:33.067: E/AndroidRuntime(16568):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at android.os.Looper.loop(Looper.java:138)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at android.app.ActivityThread.main(ActivityThread.java:3701)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at java.lang.reflect.Method.invoke(Method.java:507)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-07 15:08:33.067: E/AndroidRuntime(16568):    at dalvik.system.NativeStart.main(Native Method)
02-07 15:08:33.067: E/AndroidRuntime(16568): Caused by: android.database.sqlite.SQLiteException: no such column: has_phone_number: , while compiling: SELECT data_version, phonetic_name, contact_id, phonetic_name_style, lookup, data12, data11, mimetype, data10, data15, data14, data13, data_sync1, display_name_source, data_sync3, data_sync2, agg_presence.chat_capability AS contact_chat_capability, data_sync4, account_type, custom_ringtone, data1, status_updates.status AS status, data4, data5, data2, data3, data8, data9, data6, group_sourceid, data7, account_name, display_name, in_visible_group, display_name_alt, is_primary, contacts_status_updates.status_res_package AS contact_status_res_package, contacts_status_updates.status_ts AS contact_status_ts, raw_contact_id, times_contacted, contacts_status_updates.status AS contact_status, status_updates.status_res_package AS status_res_package, status_updates.status_icon AS status_icon, contacts_status_updates.status_icon AS contact_status_icon, version, presence.mode AS mode, last_time_contacted, _id, res_package, name_verified, is_super_primary, dirty, status_updates.status_ts AS status_ts, photo_id, send_to_voicemail, name_raw_contact_id, contacts_status_updates.status_label AS contact_status

1 个答案:

答案 0 :(得分:3)

使用此方法获取联系人数据

    Cursor phones = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null, null);
    if (phones != null) {
        while (phones.moveToNext()) {




                    String name = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String contactId=phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));


        }
        phones.close();

    }