不透明指针错误 Swift 3.1
Opaque Pointer error Swift 3.1
我在使用 Swift 3.1 将外国字符和国际字符插入或更新到 SQLite 数据库时遇到问题。
因此,我尝试在下面添加 UTF8,但是 运行 出现错误“无法将类型 'string' 的值转换为预期的参数类型 '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)
}
我每次都在搜索,但找不到对此类错误的引用。
您不需要将 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
我在使用 Swift 3.1 将外国字符和国际字符插入或更新到 SQLite 数据库时遇到问题。 因此,我尝试在下面添加 UTF8,但是 运行 出现错误“无法将类型 'string' 的值转换为预期的参数类型 '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)
}
我每次都在搜索,但找不到对此类错误的引用。
您不需要将 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