我正在尝试使用以下函数将数据保存到sqlite数据库中。
func insertSpecieDetail(_ specieId: Int)
{
//
// Create a temporary SpecieDetailModel for storing our values from the query.
//
let specieDetail: SpecieDetailModel = SpecieDetailModel()
//
// Create an instance of our statement for SQLite to use.
//
var statement: OpaquePointer? = nil
let update:String = String(format:SQL.InsertUserUpdates, specieId)
if sqlite3_prepare_v2(self.database, update, -1, &statement, nil) == SQLITE_OK {
sqlite3_bind_text(statement, 1, specieDetail.userCommonName, -1, nil);
sqlite3_bind_text(statement, 2, specieDetail.userCommonNameFR, -1, nil);
sqlite3_bind_text(statement, 3, specieDetail.userCommonNameES, -1, nil);
sqlite3_bind_text(statement, 4, specieDetail.userCommonNameDE, -1, nil);
sqlite3_bind_text(statement, 5, specieDetail.userNotes, -1, nil);
if sqlite3_step(statement) == SQLITE_DONE {
print("Successfully inserted row.")
} else {
print("Could not Update row.")
}
}
sqlite3_finalize(statement)
}
SQL.InsertUserUpdates =
"INSERT INTO RL_Species (user_common_name, user_common_name_fr, user_common_name_es, user_common_name_de, user_notes) VALUE (?, ?, ?, ?, ?)" + "WHERE RL_Species.id = %d"
没有收到任何错误,但系统在命令后跳过:
if sqlite3_prepare_v2(self.database, update, -1, &statement, nil) == SQLITE_OK
在同一行,我检查了什么"更新"等于并低于
"INSERT INTO RL_Species (user_common_name, user_common_name_fr, user_common_name_es, user_common_name_de, user_notes) VALUE (?, ?, ?, ?, ?)WHERE RL_Species.id = 8"
所以看起来该字符串正在找到specieId OK。
我错过了什么?
更新: 找到VALUE vs. VALUES错误,但仍然出现错误"在"附近WHERE
func insertSpecieDetail(_ specieId: Int)
{
//
// Create a temporary SpecieDetailModel for storing our values from the query.
//
let specieDetail: SpecieDetailModel = SpecieDetailModel()
//
// Create an instance of our statement for SQLite to use.
//
var statement: OpaquePointer? = nil
let update = "INSERT INTO RL_Species (user_common_name, user_common_name_fr, user_common_name_es, user_common_name_de, user_notes) VALUES ('\(specieDetail.commonName)', '\(specieDetail.commonNameFR)', '\(specieDetail.commonNameES)', '\(specieDetail.commonNameDE)', '\(specieDetail.userNotes)') WHERE RL_Species.id = '\(specieId)';"
if sqlite3_prepare_v2(database, update, -1, &statement, nil) == SQLITE_OK {
if sqlite3_step(statement) == SQLITE_DONE {
print("Successfully inserted row.")
} else {
print("Could not Update row.")
}
} else {
// forces SQLite to send error message
let errorMessage = String.init(validatingUTF8: sqlite3_errmsg(database))!
print("update failed! \(errorMessage)") }
sqlite3_finalize(statement)
}