从 ContactsContract 表中获取数据时出现 SQLITE 异常

Posted

技术标签:

【中文标题】从 ContactsContract 表中获取数据时出现 SQLITE 异常【英文标题】:Getting SQLITE Exception in getting data from ContactsContract table 【发布时间】:2014-03-04 16:56:23 【问题描述】:

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

这是我的源代码:

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 ComponentInfocom.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

【问题讨论】:

感谢您编辑帖子,但请帮我解决这个问题 你试过答案了吗? 是的,我现在试过了,它给了我这个错误“原因:java.lang.IllegalStateException: get field slot from row 0 col -1 failed” 【参考方案1】:

使用它来获取联系数据

    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();

    

【讨论】:

是的,我现在试过了,它给了我这个错误“原因:java.lang.IllegalStateException: get field slot from row 0 col -1 failed” 你能告诉我行号吗? if (Integer.parseInt(phones.getString(phones .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 它表示对字段插槽 0,-1 的请求错误。 numRows = 164,numColumns = 63,

以上是关于从 ContactsContract 表中获取数据时出现 SQLITE 异常的主要内容,如果未能解决你的问题,请参考以下文章

从 Android ContactsContract.Contacts 查询所有联系人的许多数据时如何提高性能?

使用 Android 上的 ContactsContract api 从联系人获取邮政地址

ContactsContract 如何枚举所有可用字段?

如何从 Android 设备中仅获取电话号码联系人

联系人列表仅使用意图 ContactsContract 显示一个电话号码

在 ContactsContract 中更新照片的功能