我有一个例程,当我的 dataModelObject 启动时,它会将 myDataBase.sqlite 文件从 App Bundle 文件夹复制到应用程序文档目录,以便应用程序可以使用它。如果它不存在,它只会复制它,这意味着在实时环境中它不会替换用户现有的数据库。该操作执行没有问题,但是目标位置的数据库与我设计的数据库完全不同。有没有其他人遇到过这个,并且可能知道是什么原因造成的?
所述数据库中的 2 个表只有一个记录在一个表中,带有一个奇怪的数字和一个 blob。
最佳答案
是的,我已确认 bundle 中的文件指向项目文件夹中正确的 .sqlite 文件。
这是我执行复制的例程,有什么特别糟糕的地方吗?我应该改用 move 吗?
class func copyFile(fileName: NSString) {
let dbPath: String = getPath(fileName)
println("copyFile fileName=\(fileName) to path=\(dbPath)")
var fileManager = NSFileManager.defaultManager()
var fromPath: String? = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent(fileName)
if !fileManager.fileExistsAtPath(dbPath) {
println("dB not found in document directory filemanager will copy this file from this path=\(fromPath) :::TO::: path=\(dbPath)")
fileManager.copyItemAtPath(fromPath!, toPath: dbPath, error: nil)
} else {
println("DID-NOT copy dB file, file allready exists at path:\(dbPath)")
}
}
class func getPath(fileName: String) -> String {
return NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0].stringByAppendingPathComponent(fileName)
}
关于ios - Swift iOS 文件管理器复制更改 sqlite 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28288028/