table raw_contacts has no column named raw_contact_id

Viewed 234

I am inserting a new contact like this in my android application: I am adding my Java Class The Error I got is Error Inserting of Data...............

I did not Understand What I am missing thats why I am facing this error

Here is My Java Class

private static void setContact(Context mContext,Account account){ 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();
            }

I am getting this error:

 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 Answers

I finally found the solution. The problem is I was using ContactsContract.RawContacts.CONTENT_URI instead of ContactsContract.Data.CONTENT_URI in the valueBackReference of Phone and Display_Name fields.

The raw contacts data is created automatically when the content matches to another entry

As soon as a raw contact is inserted or whenever its constituent data changes, the provider will check if the raw contact matches other existing raw contacts and if so will aggregate it with those. The aggregation is reflected in the RawContacts table by the change of the ContactsContract.RawContactsColumns.CONTACT_ID field, which is the reference to the aggregate contact.

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

Related