2011-10-03 196 views
0

我有一个数据插入到我的sqlite数据库的问题。我通过互联网获取JSON数据,并试图将该数据放入sqlite数据库,但它扔我constraint failes error。我知道这意味着我已经插入相同的数据,或者至少使用相同的ID,但是当我从模拟器查看数据库时,该表是空的,并且没有数据。下面是我'使用的代码:Android的Sqlite数据库 - 约束失败

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").toString()); 
        Log.i("Languages","Languages : "+langs.getJSONObject(i).getString("title").toString()); 
      } 

      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").toString()); 
       Log.w("Element","Show Statistics cats : "+stats.getJSONObject(i).getString("value").toString()); 
       Log.w("Element","Show Statistics cats : "+stats.getJSONObject(i).getString("name").toString()); 
      } 

      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","FUCKKKKKKKKKKKKKK ERROR : "+e); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     Log.w("Error","FUCKKKKKKKKKKKKKK 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类是我的数据库帮助class.I'm用它来把数据sqlite.I对其他表没有任何问题。我可以用类似的代码将它们的数据放在这里,但收藏表只是不想工作。

那么,任何建议/帮助或其他可以帮助我解决我的问题? 谢谢!

回答

0

有几件事可能会引发此异常。我认为你首先需要检查你是否插入所有的NOT NULL行,所以你可以确定所有的绑定行都被插入。

+0

实际上这是问题...谢谢你的帮助/建议 –

0

正如我们所知,Sqlite是在多个移动平台上可用的数据库的更轻版本。由于它不是一个完整的RDBMS,它支持一些数据类型,而不是全部。

请检查您是否正确插入数据。