0
我正在創建一個ContactCard應用程序,並且有寫入數據庫的錯誤。 我寫方法:Swift 3 sqlite bug
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);
}
如果我寫方法後,讀的是這樣的輸出:
一個值是正確的,那就是「日期」值。其他值都不正確。有人想法?
我的閱讀方法工作,因爲我有我的數據庫中的標準的一個記錄,並且工作正常。
讀取方法:
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)
}
}
你有沒有例子? –
或者你可以在我的代碼中添加代碼?我會欣賞:) –
@AbusiveVids在調用sqlite3_bind_text時,只需在第五個參數中用'SQLITE_TRANSIENT'替換'nil'。 – rmaddy