2014-07-02 41 views
0

這是代碼,我試圖插入到我的表中,並得到一個例外,列ShopName(COL_SN)是不唯一的,雖然我給一個不存在的名稱數據庫。該特定列是表的主鍵列的列名不唯一(代碼19)

public void insert(String sn,String skn,String sa,String un,String pwd) throws SQLiteConstraintException 
     { 
      sdb=this.getWritableDatabase(); 
      System.out.println("in insert method"); 
      //sdb.execSQL("insert into " + TABLE_ShopDetails + " values(" +sn+ "," +skn+ "," +sa+ "," +un+ "," +pwd+ ")"); 
      ContentValues cv=new ContentValues(); 
      cv.put(COL_SN,sn); 
      cv.put(COL_SKN,skn); 
      cv.put(COL_SA,sa); 
      cv.put(COL_UN,un); 
      cv.put(COL_PWD,pwd); 
      sdb.insert(TABLE_ShopDetails,COL_SN,cv); 
      sdb.insert(TABLE_ShopDetails,COL_SKN,cv); 
      sdb.insert(TABLE_ShopDetails,COL_SA,cv); 
      sdb.insert(TABLE_ShopDetails,COL_UN,cv); 
      sdb.insert(TABLE_ShopDetails,COL_PWD,cv); 
     } 
+0

的可能重複的[SQLite的錯誤「列\ _id不是唯一的」插入一個空表上時(http://stackoverflow.com/questions/15731865/sqlite-error-column-id -is-不唯一-上時-插入 - 進入 - 一個空表) –

回答

1

就叫插入一次

sdb.insert(TABLE_ShopDetails,空,CV);

0

您應該只撥打insert()一次。

ContentValues對象已包含所有列的值。通過插入多次,您試圖創建重複記錄,這會導致主鍵違規。

第二個參數可以爲null,它僅適用於特殊情況(值爲空時)。

0

你絕對只需要像其他人說的那樣調用插入一次。第二個可選參數應該最有可能爲空,它是以下...

optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.

而且你可能要考慮建立一個內容提供商。下面的鏈接將作爲一個很好的教程。

Android SQLite database and content provider - Tutorial