gpt4 book ai didi

swift - 更新表不适用于真实设备 (fmdb)

转载 作者:行者123 更新时间:2023-11-30 10:03:19 26 4
gpt4 key购买 nike

我是 iOS 开发新手,我遇到了一个问题。我正在尝试使用 FMDB 更新表,但尽管它在所有模拟器中都有效,但在真实设备上却不起作用。我确信数据库已传输到设备,因为所有选择查询都正常运行。另一方面,所有更新查询都不是。我尝试过使用 NSNumber 或 NSInteger 但...什么也没有。

if (database.open())
{
let rs = database.executeQuery("update \(TABLE_NAME) set x=1 where id=\(y.getId())",withArgumentsInArray: nil)
//database.executeUpdate("update \(TABLE_NAME) set x=1 where id=?", withArgumentsInArray:[NSInteger(y.getId())])
database.close()
}

以上解决方案均无效。

有什么帮助吗?

最佳答案

问题在于 db 文件位于只读资源包中。当我将 db 文件复制到 Documents 文件夹时,一切正常。

我在 http://www.theappguruz.com/blog/use-sqlite-database-swift 上找到

这段代码

class func copyFile(fileName: NSString) {
let dbPath: String = getPath(fileName as String)
let fileManager = NSFileManager.defaultManager()
if !fileManager.fileExistsAtPath(dbPath) {

let documentsURL = NSBundle.mainBundle().resourceURL
let fromPath = documentsURL!.URLByAppendingPathComponent(fileName as String)

var error : NSError?
do {
try fileManager.copyItemAtPath(fromPath.path!, toPath: dbPath)
} catch let error1 as NSError {
error = error1
}
let alert: UIAlertView = UIAlertView()
if (error != nil) {
alert.title = "Error Occured"
alert.message = error?.localizedDescription
} else {
alert.title = "Successfully Copy"
alert.message = "Your database copy successfully"
}
alert.delegate = nil
alert.addButtonWithTitle("Ok")
alert.show()
}
}

UIAlertView 的代码是额外的,您可以省略它。

关于swift - 更新表不适用于真实设备 (fmdb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37307029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com