I have an sqlite3 table, every time I run my code, it adds multiple entries. How can I change this query to add only one entry?
CREATE TABLE "GroupTable" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"memberId" TEXT NOT NULL,
"adminId" TEXT NOT NULL,
"name" TEXT NOT NULL
);
The method below shows how I am adding data:
func addDatatoList(info: ListModel ) -> Bool
{
sharedInstance.database!.open()
let isInserted = sharedInstance.database!.executeUpdate("INSERT or REPLACE INTO GroupTable(adminId, memberId, name) VALUES (?,?,?)", withArgumentsIn: [ info.adminId,info. memberId,info.name])
sharedInstance.database!.close()
return isInserted
}
You’re not supplying an
idvalue, soINSERT OR REPLACEwill never replace. You theoretically could addidparameter, passingNSNull()if theidvalue is NULLThat having been said, if you did
INSERT, you probably want to retrieve the row’s auto increment id value:But, then again, if you're now programmatically determining whether you inserted or updated, you might as well have two SQL statements, one for
INSERTand one forUPDATE.