Android Sqlite database - constraint failes
I have a problem with inserting data in my sqlite database. I'm getting JSON data over the internet and then trying to put that data in sqlite database, but it's throwing me an constraint fails error
. I know that means I've already inserted the same data, or at least with the same id, but when I look at my database from emulator, that table is empty and there is no data. Here is the code that I'am using :
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;
}
and here is the exception which I got :
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(S开发者_StackOverflow社区ynchronization.java:434)
10-03 09:07:48.899: ERROR/Database(30136): at java.lang.Thread.run(Thread.java:1102)
UserDbHelper
class is my database helper class. I'm using it to put data in sqlite. I don't have any problems with other tables. I can put data on them with similar code to this, but collections table just doesn't want to work..
So any suggestions/help or something else which can help me to solve my problem? Thanks!
As we know Sqlite is a lighter version of the database which is available on the multiple mobile platforms. Since it is not a full fledged RDBMS it supports some datatypes and not all of them.
Please check whether you are inserting data appropriately.
There are a few things that can throw this exception. I think you first need o check if you are inserting all NOT NULL
rows, so you can be sure that all binding rows are inserted.
精彩评论