我真的很困惑如何在Swift中使用sqlite。我的INSERT查询执行没有任何错误,但是当我打开数据库时,应该添加的数据不存在。我正在使用FMDB包装器。
这是我的代码:
let documentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let path = documentsFolder.stringByAppendingPathComponent("currencylite.db")
let database = FMDatabase(path: path)
if !database.open() {
println("Unable to open database")
return
} else {
if database.open() {
let querySQL = "INSERT INTO currencyrates (baseccy, matchccy, rateccy, date) VALUES ('\(base)', '\(currency)', \(frate), '\(updtdate)')"
println(querySQL)
database.executeQuery(querySQL, withArgumentsInArray: nil)
}
println(querySQL)
回声INSERT INTO currencyrates (baseccy, matchccy, rateccy, date) VALUES ('AUD', 'ZAR', 9.3683, '2015-06-25')
所以我很确定它不是来自数据,而是来自我执行查询的方式......
编辑:
执行查询后,我查看了sqlite数据库中项目文件夹中的文件夹和文件夹文件夹中的文件夹,两者都没有得到更新......我可以阅读内容很好,但我不能这样做插入......
感谢您的帮助!
答案 0 :(得分:0)
你应该这样做:
let documentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let path = documentsFolder.stringByAppendingPathComponent("currencylite.db")
let database = FMDatabase(path: path)
if !database.open() {
println("Unable to open database")
return
} else {
if database.open() {
let querySQL = "INSERT INTO currencyrates (baseccy, matchccy, rateccy, date) VALUES (?, ?, ?, ?)"
println(querySQL)
database.executeQuery(querySQL, baseccy, matchccy, NSNumber(double: rateccy), date)
}
假设:
baseccy: String
,matchccy: String
,rateccy: Double
,date: String
答案 1 :(得分:0)
试试这个:
if !database.executeUpdate("insert into currencyrates (baseccy, matchccy, rateccy, date) values (?, ?, ?, ?)", withArgumentsInArray: [base, currency, NSNumber(double: frate), updtdate]) {
println("insert 1 table failed: \(database.lastErrorMessage())")
}