问题描述
我想建立一个ContentProvider的Android应用中保存有关房屋的信息。我希望能够设置_ID场是相同ID字段在我的外部数据库(程序将同步与此外部数据库的ContentProvider)。
这样做的原因是,所以我想能够引用ID,如:
内容://com.example.acme.propertyprovider/properties/23
,其中图23是在两个外部数据库和内部数据库相同的ID。
下面只是导致错误android.database.SQLException:无法插入行内容://com.example.acme.propertyprovider/properties
ContentValues值=新ContentValues();values.put(Properties._ID,23);
values.put(Properties.COLUMN_NAME_ROAD,123样品物业);
values.put(Properties.COLUMN_NAME_RENT,320);。getContentResolver()插入(Properties.CONTENT_URI,价值观);
您会更好设置在Android数据库中的外键从外部数据库保存ID。然后,你就不必担心买不同步的东西。由于_ID字段是自动递增,你会为自己要创建一个噩梦。
添加类似extHouse_id(或其他)。这是一个非常普遍的做法。
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.
这篇关于设置使用SQLite和放大器的_ID场; ContentProvider的Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!