我正在创建一个ContactCard应用程序,我有一个写数据库的错误。
我的写作方法:

func create(contact: Contact) {

    let query = "INSERT INTO contact (Voornaam, Achternaam, Telefoonnummer, Email, Adres, Huisnummer, Toevoeging, Woonplaats, Postcode, Geboortedatum, Afbeelding) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
    var statement : OpaquePointer? = nil

    let jpegCompressionQuality: CGFloat = 0.9 // Set this to whatever suits your purpose

    if let base64String = UIImageJPEGRepresentation((contact.image), jpegCompressionQuality)?.base64EncodedString(){
        if sqlite3_prepare(db, query, -1, &statement, nil) == SQLITE_OK {
            sqlite3_bind_text(statement, 1, contact.firstName, -1, nil);
            sqlite3_bind_text(statement, 2, contact.lastName, -1, nil);
            sqlite3_bind_text(statement, 3, contact.phone, -1, nil);
            sqlite3_bind_text(statement, 4, contact.email, -1, nil);
            sqlite3_bind_text(statement, 5, contact.street, -1, nil);
            sqlite3_bind_text(statement, 6, contact.houseNumber, -1, nil);
            sqlite3_bind_text(statement, 7, contact.toevoeging, -1, nil);
            sqlite3_bind_text(statement, 8, contact.city, -1, nil);
            sqlite3_bind_text(statement, 9, contact.zip, -1, nil);
            sqlite3_bind_text(statement, 10, contact.date, -1, nil);
            sqlite3_bind_text(statement, 11, base64String, -1, nil);

            contacten.append(contact)

            if sqlite3_step(statement) != SQLITE_DONE {
                print("Error inserting row")
            }
        } else {
            print("Error")
        }
    }

    sqlite3_reset(statement);
    sqlite3_finalize(statement);
}

如果在write方法之后读取,则输出:
swift - Swift 3 sqlite bug-LMLPHP
有一个值是正确的,那就是“日期”值。其他值都不正确。有人有主意吗?
我的阅读方法之所以有效,是因为我的数据库中有一条标准的记录,而且效果很好。
阅读方法:
func read() {

    let query = "SELECT * FROM contact;"

    var statement : OpaquePointer? = nil
    if sqlite3_prepare_v2(db, query, -1, &statement, nil) != SQLITE_OK {
        let errmsg = String(cString: sqlite3_errmsg(db))
        print("error query: \(errmsg)")
    }

    while sqlite3_step(statement) == SQLITE_ROW {
        let contact = Contact()

        contact.firstName = String(cString: sqlite3_column_text(statement, 0))
        contact.lastName = String(cString: sqlite3_column_text(statement, 1))
        contact.phone = String(cString: sqlite3_column_text(statement, 2))
        contact.email = String(cString: sqlite3_column_text(statement, 3))
        contact.street = String(cString: sqlite3_column_text(statement, 4))
        contact.houseNumber = String(cString: sqlite3_column_text(statement, 5))
        contact.toevoeging = String(cString: sqlite3_column_text(statement, 6))
        contact.city = String(cString: sqlite3_column_text(statement, 7))
        contact.zip = String(cString: sqlite3_column_text(statement, 8))
        contact.date = String(cString: sqlite3_column_text(statement, 9))
        let imageData = NSData(base64Encoded: String(cString: sqlite3_column_text(statement, 10)), options: .ignoreUnknownCharacters)

        if (imageData?.length)! > 0{
            contact.image = UIImage(data: imageData as! Data)!
        }
        contacten.append(contact)
    }
}

最佳答案

sqlite3_bind_text()的第五个参数不能是nil
除非您知道得更清楚,否则请使用SQLITE_TRANSIENT

关于swift - Swift 3 sqlite bug,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40110405/

10-10 09:38