I’m trying to set up a ContentProvider in an Android app to hold information about houses. I’d like to be able to set the _ID field to be the same as the ID field in my external database (the program will sync the ContentProvider with this external database).
The reason for this is so I would like to be able to reference the ID like:
content://com.example.acme.propertyprovider/properties/23
where 23 is the same ID in both the external database and the internal database.
The following just causes the error “android.database.SQLException: Failed to insert row into content://com.example.acme.propertyprovider/properties
ContentValues values = new ContentValues();
values.put(Properties._ID, 23);
values.put(Properties.COLUMN_NAME_ROAD, "123 Sample Property");
values.put(Properties.COLUMN_NAME_RENT, 320);
getContentResolver().insert(Properties.CONTENT_URI, values);
You’d be better off setting a foreign key in the Android database to hold the id from your external database. Then you don’t have to worry about things getting out of sync. Since the _ID fields are auto-incrementing you’ll be creating a nightmare for yourself.
Add something like extHouse_id (or whatever). That’s a very common practice.