sqliteswift3opaque-pointers

Opaque Pointer error Swift 3.1


I am having an issue with foreign and international characters inserted or updated into a SQLite database using Swift 3.1. So, I tried the below to add the UTF8, but run into an error " cannot convert value of type 'string' to expected argument type 'OpaquePointer!' ".

I am stumped and not sure where to look.

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)
}

I am search every, but cannot find a reference to this type of error.


Solution

  • You don't need to turn your Swift strings into C strings:

    sqlite3_bind_text(update, 1, diveMaster, -1, SQLITE_TRANSIENT)
    

    Besides, beware: the sqlite3_bind_xxx functions accept 1-based indexes: sqlite3_bind_text(update, 0, ...) is invalid. See https://sqlite.org/c3ref/bind_blob.html

    Reference: https://github.com/groue/GRDB.swift/blob/v0.106.2/GRDB/Core/Statement.swift#L162