Reputation: 55
this is the code where I am trying to insert into my table and getting an exception that column ShopName(COL_SN) is not unique though I am giving a name that is not already existing in the database.That particular column is the primary key of the table
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);
}
Upvotes: 0
Views: 947
Reputation: 232
You definitely only need to call insert once as others have said. The second optional parameter should most likely be null, it is for the following ...
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.
Also you might want to look into setting up a content provider. The link below would serve as a great tutorial.
Android SQLite database and content provider - Tutorial
Upvotes: 0
Reputation: 2619
just call insert only once
sdb.insert(TABLE_ShopDetails,null,cv);
Upvotes: 1
Reputation: 55340
You should call insert()
only once.
The ContentValues
object already contains the values for all columns. By inserting multiple times, you're trying to create duplicate records, which results in a primary key violation.
The second parameter can be null, it's only for special cases (when values is empty).
Upvotes: 0