表raw_contacts没有名为raw_contact_id的列

时间:2019-06-20 05:34:26

标签: sqlite android-sqlite sql-insert contacts

我将这样的新联系人插入我的android应用程序中: 我正在添加Java类我遇到的错误是数据插入错误......

我没有理解我所缺少的,这就是为什么我遇到这个错误

这是我的Java类

private static void setContact(上下文mContext,帐户帐户){         ContentResolver contentResolver = mContext.getContentResolver();

        int rawContactInsertIndex = operationList.size();


            operationList.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI,true))
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,account.type)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME,account.name)
                    .build());

            operationList.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI,true))
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,"Alex")
                    .build());

            operationList.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI,true))
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"+99999999")
                    .build());

            operationList.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE,"vnd.android.cursor.item/vnd.com.meetingapp.android.profile")
                    .withValue(ContactsContract.Data.DATA1,"Meet App")
                    .withValue(ContactsContract.Data.DATA2,"Meet App")
                    .withValue(ContactsContract.Data.DATA3,"Meet App")
                    .build());


            try {
                contentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
                Log.i("inPerformSync", "successfully registered!");
            } catch (Exception e) {
                Log.i("inPerformSync", "register failed "+e.getMessage());
                e.printStackTrace();
            }

我收到此错误:

 E/SQLiteLog: (1) table raw_contacts has no column named raw_contact_id
06-20 10:23:54.819 1610-11666/? E/SQLiteDatabase: Error inserting 
raw_contact_id=79253 pinned=0 contact_id=null data1=Alex account_id=1 mimetype=vnd.android.cursor.item/name
    android.database.sqlite.SQLiteException: table raw_contacts has no column named raw_contact_id (code 1): , while compiling: INSERT INTO raw_contacts(raw_contact_id,pinned,contact_id,data1,account_id,mimetype) VALUES (?,?,?,?,?,?)
    #################################################################
    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
        (table raw_contacts has no column named raw_contact_id (code 1): , while compiling: INSERT INTO raw_contacts(raw_contact_id,pinned,contact_id,data1,account_id,mimetype) VALUES (?,?,?,?,?,?))
    #################################################################
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1607)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
        at com.android.providers.contacts.ContactsProvider2.insertRawContact(ContactsProvider2.java:4975)
        at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:4174)
        at com.android.providers.contacts.AbstractContactsProvider.insert(AbstractContactsProvider.java:139)
        at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:3441)
        at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:307)
        at com.android.providers.contacts.AbstractContactsProvider.applyBatch(AbstractContactsProvider.java:240)
        at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:3721)
        at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:328)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:192)
        at android.os.Binder.execTransact(Binder.java:453)
06-20 10:23:54.829 1610-11666/? E/DatabaseUtils: Writing exception to parcel
    android.content.OperationApplicationException: insert failed
        at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:309)
        at com.android.providers.contacts.AbstractContactsProvider.applyBatch(AbstractContactsProvider.java:240)
        at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:3721)
        at 

android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:328)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:192)
        at android.os.Binder.execTransact(Binder.java:453)

2 个答案:

答案 0 :(得分:0)

当内容与另一个条目匹配时,将自动创建原始联系人数据

  

一旦插入原始联系人或每当其组成数据发生更改时,提供程序就会检查该原始联系人是否与其他现有的原始联系人匹配,如果匹配,则将其与其他原始联系人进行汇总。通过更改ContactsContract.RawContactsColumns.CONTACT_ID字段将汇总反映在RawContacts表中,该字段是对汇总联系人的引用。

https://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html#aggregation

答案 1 :(得分:0)

我终于找到了解决方案。问题是我在ContactsContract.RawContacts.CONTENT_URIContactsContract.Data.CONTENT_URI字段的valueBackReference中使用Phone而不是Display_Name