我是swift的新手,只是遵循了sqlite的教程,它运行得很好,但升级到swift 2.1之后,它将“可选”添加到我的结果中,我该如何摆脱它?
查找数据功能
@IBAction func findContact(sender: UIButton)
{
if database.open() {
let querySQL = "SELECT address, phone FROM CONTACTS WHERE name = '\(name.text)'"
let results:FMResultSet? = database.executeQuery(querySQL,withArgumentsInArray: nil)
if results!.next() == true {
if let address1=results!.stringForColumn("address")
{
address.text = address1
}
phone.text = results!.stringForColumn("phone")
status.text = "Record Found"
} else {
status.text = "Record not found"
address.text = ""
phone.text = ""
}
database.close()
} else {
print("Error: \(database.lastErrorMessage())", terminator: "")
}
}
保存数据功能
@IBAction func saveData(sender: UIButton)
{
let database = FMDatabase(path: databasePath as String)
if database.open() {
let insertSQL = "INSERT INTO CONTACTS (name, address, phone) VALUES ('\(name.text)', '\(address.text)', '\(phone.text)')"
let result = database.executeUpdate(insertSQL,
withArgumentsInArray: nil)
if !result {
status.text = "Failed to add contact"
print("Error: \(database.lastErrorMessage())", terminator: "")
} else {
status.text = "Contact Added"
name.text = ""
address.text = ""
phone.text = ""
}
} else {
print("Error: \(database.lastErrorMessage())", terminator: "")
}
}
答案 0 :(得分:1)
问题不在于您选择数据时。问题在于您插入数据时。在插入时您只需在表格中插入可选值,如地址字段和电话号码,如屏幕截图所示......所以,在插入时,您需要打开该可选值以避免optional()
关键字。
答案 1 :(得分:0)
let querySQL = "SELECT address, phone FROM CONTACTS WHERE name = '\(name.text)'"
你需要添加name.text ..
的感叹号结尾ecs: (name.text!)
可能会帮助你。