2011-10-03 131 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,它支持一些數據類型,而不是全部。

請檢查您是否正確插入數據。