【发布时间】:2011-10-03 06:22:09
【问题描述】:
我在我的 sqlite 数据库中插入数据时遇到问题。我正在通过 Internet 获取 JSON 数据,然后尝试将该数据放入 sqlite 数据库中,但它给了我一个constraint fails error。我知道这意味着我已经插入了相同的数据,或者至少具有相同的 id,但是当我从模拟器查看我的数据库时,该表是空的并且没有数据。这是我正在使用的代码:
public boolean executeInsert() {
UserDatabaseHelper userDbHelper = new UserDatabaseHelper(context, null, 1);
userDbHelper.initialize(context);
ContentValues values = new ContentValues();
values.put("objectId", objectId);
Log.i("objectd Id ", "object ID : " + objectId);
values.put("objectOid", objectOid);
Log.i("objectd Oid ", "object OID : " + objectOid);
try {
String jsonData = new String(collectionBuffer, "UTF-8");
Log.w("JSONDATA", "JSONDATA VALID OR NOT : " + jsonData);
json = new JSONObject(jsonData);
JSONObject jsonObj = (JSONObject) new JSONTokener(jsonData).nextValue();
locale = jsonObj.getString("locale"); // don't put in database
Log.i("Locale", "Locale : " + locale);
id = Integer.parseInt(jsonObj.getString("id"));
Log.i("Id", "Id : " + id);
genreId = Integer.parseInt(jsonObj.getString("genre_id"));
Log.i("Genre ID ", "Genre Id : " + genreId);
values.put("genreId", genreId); //genreId
dateCreated = jsonObj.getString("date_created");
Log.i("date Created", "Date Created : " + dateCreated);
values.put("dateCreated", dateCreated);
title = jsonObj.getString("title");
Log.i("title", "title : " + title);
values.put("title", title);
isRecommended = Integer.parseInt(jsonObj.getString("is_recommended"));
Log.i("Is Recommended", "IS Recommended : " + isRecommended);
values.put("isRecommended", isRecommended);
userCount = Integer.parseInt(jsonObj.getString("subscribed_users_count"));
Log.i("USubscribed Users Count", "Subscribed Users Count : " + userCount);
values.put("usersCount", userCount);
envelopeCost = Double.parseDouble(jsonObj.getString("envelope_cost"));
Log.i("envelope cost", "envelope cost" + envelopeCost);
values.put("envelopeCost", envelopeCost);
alias = jsonObj.getString("alias");
Log.i("alias", "alias : " + alias);
values.put("alias", alias);
imageWidth = Integer.parseInt(jsonObj.getString("category_big_image_width"));
Log.i("category_big_image_width", "category_big_image_width : " + imageWidth);
totalCardsCount = Integer.parseInt(jsonObj.getString("total_cards_count"));
Log.i("Total Cards Count", "Total Cards Count : " + totalCardsCount);
values.put("cardsCount", totalCardsCount);
ownedCardsCount = Integer.parseInt(jsonObj.getString("owned_cards_count"));
Log.i("Owner Cards Count", "Owned Cards Count : " + ownedCardsCount);
values.put("ownedCardsCount", ownedCardsCount);
elemOrder = Integer.parseInt(jsonObj.getString("elem_order"));
Log.i("elem order", "elem order : " + elemOrder);
values.put("elemOrder", elemOrder);
intro = jsonObj.getString("intro_text");
Log.i("Intro text", "Intro text : " + intro);
values.put("introText", intro);
createdBy = jsonObj.getString("created_by");
Log.i("created By", "Created By : " + createdBy);
values.put("createdBy", createdBy);
right = jsonObj.getString("reserved_rights_to");
Log.i("reserved_rights_to", "reserved_rights_to : " + right);
values.put("reservedRightsTo", right);
legals = jsonObj.getString("legal_notice");
Log.i("legals", "legals : " + legals);
values.put("legalNotice", legals);
isSubscribed = Integer.parseInt(jsonObj.getString("is_subscribed"));
Log.i("is subscribed", "Is subcribed : " + isSubscribed);
values.put("isSubscribed", isSubscribed);
cardsPerEnvelop = Integer.parseInt(jsonObj.getString("cards_per_envelope"));
Log.i("Cards per envelope", "Carda per envelope : " + cardsPerEnvelop);
values.put("cardsPerEnvelope", cardsPerEnvelop);
JSONArray langs = jsonObj.getJSONArray("languages");
for (int i = 0; i < langs.length(); i++) {
Log.i("Languages", "Languages : " + langs.getJSONObject(i).getString("locale"));
Log.i("Languages", "Languages : " + langs.getJSONObject(i).getString("title"));
}
tagTitle = jsonObj.getString("tag_title");
Log.i("tag title", "tag title : " + tagTitle);
values.put("tagTitle", tagTitle);
categoryTitle = jsonObj.getString("category_title");
Log.i("category title", "category title : " + categoryTitle);
values.put("categoryTitle", categoryTitle);
dateTitle = jsonObj.getString("date_title");
Log.i("date title", "date title : " + dateTitle);
values.put("dateTitle", dateTitle);
JSONArray stats = jsonObj.getJSONArray("statistics_cats");
for (int i = 0; i < stats.length(); i++) {
//String row = stats.get(i).toString();
Log.w("Element", "Show Statistics cats : " + stats.getJSONObject(i).getString("type"));
Log.w("Element", "Show Statistics cats : " + stats.getJSONObject(i).getString("value"));
Log.w("Element", "Show Statistics cats : " + stats.getJSONObject(i).getString("name"));
}
isEnabled = Integer.parseInt(jsonObj.getString("is_enabled"));
Log.i("is enabled", "is enabled : " + isEnabled);
values.put("isEnabled", isEnabled);
hasOwnerContent = Integer.parseInt(jsonObj.getString("has_owned_content"));
Log.i("has owned content", "has owned content : " + hasOwnerContent);
values.put("hasOwnedContent", hasOwnerContent);
isCommingSoon = Integer.parseInt(jsonObj.getString("is_coming_soon"));
Log.i("is comming soon", "is comming soon : " + isCommingSoon);
isPublic = Integer.parseInt(jsonObj.getString("is_public"));
Log.i("is public", "is public : " + isPublic);
//values.put("isPublic", isPublic);
visibleCountries = jsonObj.getString("visible_countries");
Log.i("visible countries", "visible countries : " + visibleCountries);
visibleLanguages = jsonObj.getString("visible_languages");
Log.i("visible languages", "visible languages : " + visibleLanguages);
columnTitle1 = jsonObj.optString("column_title_1");
Log.i("column title 1", "columtn title 1 : " + columnTitle1);
values.put("columnTitle1", columnTitle1);
columnTitle2 = jsonObj.optString("column_title_2");
Log.i("column title 2", "columtn title 2 : " + columnTitle2);
values.put("columnTitle2", columnTitle2);
columnTitle3 = jsonObj.optString("column_title_3");
Log.i("column title 3", "columtn title 3 : " + columnTitle3);
values.put("columnTitle3", columnTitle3);
columnTitle4 = jsonObj.optString("column_title_4");
Log.i("column title 4", "columtn title 4 : " + columnTitle4);
values.put("columnTitle4", columnTitle4);
columnTitle5 = jsonObj.optString("column_title_5");
Log.i("column title 5", "columtn title 5 : " + columnTitle5);
values.put("columnTitle5", columnTitle5);
String sql = "SELECT * FROM collections WHERE objectId = " + objectId;
Cursor mCursor = userDbHelper.executeSQLQuery(sql);
if (mCursor.getCount() == 0) {
userDbHelper.executeQuery("collections", values);
mCursor.close();
} else {
for (mCursor.moveToFirst(); mCursor.moveToNext(); mCursor.isAfterLast()) {
int mObjectId = mCursor.getInt(mCursor.getColumnIndex("objectId"));
if (objectId != mObjectId) {
userDbHelper.executeQuery("collections", values);
}
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.w("Error", "ERROR : " + e);
} catch (JSONException e) {
e.printStackTrace();
Log.w("Error", "ERROR : " + e);
} finally {
userDbHelper.close();
}
return true;
}
这是我得到的例外:
10-03 09:07:48.899: ERROR/Database(30136): Error inserting cardsPerEnvelope=1 hasOwnedContent=1 legalNotice=legals elemOrder=4 dateTitle=Datee tagTitle=Taggv title=Nimasystems 11er objectId=6 introText=txt cardsCount=2 isRecommended=1 usersCount=3 isSubscribed=1 columnTitle1= categoryTitle=Catt alias=nima1r envelopeCost=3.0 ownedCardsCount=1 isEnabled=1 objectOid=00529a1c5597334a96e337feda879831 createdBy=created ny reservedRightsTo=nonono dateCreated=2011-09-27 columnTitle3= columnTitle2= columnTitle5= columnTitle4= genreId=3
10-03 09:07:48.899: ERROR/Database(30136): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
10-03 09:07:48.899: ERROR/Database(30136): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
10-03 09:07:48.899: ERROR/Database(30136): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
10-03 09:07:48.899: ERROR/Database(30136): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1623)
10-03 09:07:48.899: ERROR/Database(30136): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1484)
10-03 09:07:48.899: ERROR/Database(30136): at com.stampii.stampii.comm.rpc.UserDatabaseHelper.execQuery(UserDatabaseHelper.java:252)
10-03 09:07:48.899: ERROR/Database(30136): at com.stampii.stampii.comm.rpc.UserDatabaseHelper.executeQuery(UserDatabaseHelper.java:247)
10-03 09:07:48.899: ERROR/Database(30136): at com.stampii.stampii.comm.rpc.CollectionRPCPacket.executeInsert(CollectionRPCPacket.java:441)
10-03 09:07:48.899: ERROR/Database(30136): at com.stampii.stampii.synchronization.Synchronization$2.run(Synchronization.java:434)
10-03 09:07:48.899: ERROR/Database(30136): at java.lang.Thread.run(Thread.java:1102)
UserDbHelper 类是我的数据库助手类。我正在使用它将数据放入 sqlite 中。我对其他表没有任何问题。我可以使用与此类似的代码将数据放在它们上,但集合表只是不想工作..
那么有什么建议/帮助或其他可以帮助我解决问题的方法吗? 谢谢!
【问题讨论】: