2014-07-02 40 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