我遇到使用Swift 3.1插入或更新到SQLite数据库中的外国和国际字符的问题。 所以,我尝试下面添加UTF8,但遇到错误“无法将类型'字符串'的值转换为预期的参数类型'OpaquePointer!' ”
我很难过,不知道该往哪里看。
func saveDiveDetails (_ dives: Dives) -> Int32 {
let diveLog = AppDelegate.getDLDatabase()
let diveBuddy = diveBuddyTextField.text ?? ""
let diveMaster = diveBuddyTextField.text ?? ""
let boatName = diveBuddyTextField.text ?? ""
let diveCenter = diveBuddyTextField.text ?? ""
let boatOperator = diveBuddyTextField.text ?? ""
let city = diveBuddyTextField.text ?? ""
let country = diveBuddyTextField.text ?? ""
let bodyOfWater = diveBuddyTextField.text ?? ""
let tripName = diveBuddyTextField.text ?? ""
var statement: OpaquePointer? = nil
let update:String = String(format: "UPDATE Dives SET dive_buddy = ?, dive_master = ?, boatName_name = ?, diveCenter_name = ?, boatOperator_name = ?, diveCity_name = ?, bodyOfWater_name = ?, diveCountry_name = ?, tripName_name = ? WHERE id = %d")
if(sqlite3_prepare_v2(self.dlDatabase.database, update, -1, &statement, nil) == SQLITE_OK){
sqlite3_bind_text(update, 0, diveBuddy.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_text(update, 1, diveMaster.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_text(update, 2, boatName.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_text(update, 3, diveCenter.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_text(update, 4, boatOperator.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_text(update, 5, city.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_text(update, 6, bodyOfWater.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_text(update, 7, country.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_text(update, 8, tripName.cString(using: String.Encoding.utf8), -1, SQLITE_TRANSIENT)
sqlite3_bind_int(update, 9, self.diveNumber)
}
sqlite3_prepare_v2(self.dlDatabase.database, update, -1, &statement, nil)
sqlite3_bind_int(statement, 1, Int32(self.diveNumber));
if sqlite3_step(statement) == SQLITE_DONE {
print("SQLite saved")
}
else {
let errorMessage = String.init(validatingUTF8: sqlite3_errmsg(dlDatabase.database))!
print("update failed! \(errorMessage)")
}
sqlite3_finalize(statement)
}
我每次搜索,但无法找到对此类错误的引用。
答案 0 :(得分:0)
您不需要将Swift字符串转换为C字符串:
sqlite3_bind_text(update, 1, diveMaster, -1, SQLITE_TRANSIENT)
此外,请注意:sqlite3_bind_xxx函数接受基于1的索引:sqlite3_bind_text(update, 0, ...)
无效。见https://sqlite.org/c3ref/bind_blob.html
参考:https://github.com/groue/GRDB.swift/blob/v0.106.2/GRDB/Core/Statement.swift#L162