This is in my first view controller, it creates a table called event and tries to insert a row.
I delete my app and install. then I run..
my log then says:
Failure: UNIQUE constraint failed: event.eid: INSERT INTO event (eid,passcode) VALUES (1,0);
There is nothing in the table it is empty. Why does it say my Unique Constraint (primary Key) is failing?
it also inserts the row, even though it throws an error?
many thanks for your help!
///////////////////////////
//creating event table
if sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS event (eid INTEGER PRIMARY KEY,
eventid INTEGER NOT NULL DEFAULT 0, passcode TEXT NOT NULL DEFAULT '',
lastTicket INTEGER NOT NULL DEFAULT 0, online INTEGER NOT NULL DEFAULT 0,
onsale INTEGER NOT NULL DEFAULT 0, lastscanned INTEGER NOT NULL DEFAULT 0,
wasOffline INTEGER NOT NULL DEFAULT 0, eventOver TEXT NOT NULL DEFAULT '',
avatar INTEGER NOT NULL DEFAULT 0, devID TEXT NOT NULL DEFAULT '',
localScans INTEGER NOT NULL DEFAULT 0)", nil, nil, nil) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
log.error("error creating table: \(errmsg)")
}
var sqlString = "SELECT * FROM event WHERE eid=1"
if(getCount(sqlString:sqlString)==0) {
let sqlInserString = "INSERT INTO event (eid,passcode) VALUES (1,0);"
log.verbose(runSQL(sqlString:sqlInserString))
}///if eid!
func runSQL(sqlString:String) -> (String) {
var sqlMessage = ""
var sqlPointer: OpaquePointer? = nil
if sqlite3_prepare_v2(db, sqlString, -1, &sqlPointer, nil) == SQLITE_OK {
if sqlite3_step(sqlPointer) == SQLITE_DONE {
sqlMessage = "Success: " + sqlString
} else {
let errmsg = String(cString: sqlite3_errmsg(db)!)
sqlMessage = "Failure: \(errmsg): " + sqlString
}
} else {
let errmsg = String(cString: sqlite3_errmsg(db)!)
sqlMessage = "Not prepared: \(errmsg): " + sqlString
}
sqlite3_finalize(sqlPointer)
return(sqlMessage)
}
eidneeds to be unique. You can't run the queryINSERT INTO event (eid,passcode) VALUES (1,0);more than once without the error. - rmaddy