【问题标题】:Swift iOS filemanager copy changes sqlite fileSwift iOS 文件管理器复制更改 sqlite 文件
【发布时间】:2015-04-02 00:09:11
【问题描述】:

我有一个例程,当我的 dataModelObject 启动时,它会将 myDataBase.sqlite 文件从 App Bundle 文件夹复制到应用程序文档目录,以便应用程序可以使用它。如果它不存在,它只会复制它,这意味着在实时环境中它不会替换用户现有的数据库。操作执行没有问题,但是目标位置的数据库与我设计的数据库完全不同。有没有其他人遇到过这种情况,并且可能知道是什么原因造成的?

所述数据库中有 2 个表,其中一个仅记录在一个表中,带有一个奇怪的数字和一个 blob。

【问题讨论】:

  • 我在我的应用程序开始时做了类似的事情,我的数据库没有问题。您确定要进入捆绑包的数据库以及您要复制的数据库是您认为的那样吗?

标签: ios sqlite nsfilemanager


【解决方案1】:

是的,我已确认包中的文件指向项目文件夹中正确的 .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)
    }

【讨论】:

  • 我找到了解决方案。上面的例程正确处理文件但是我安装了一个相机应用程序,我不小心用它打开了当时损坏数据库的 .sqlite 文件。所以我再次清理了添加到我的包中的数据库,然后副本工作正常。
猜你喜欢
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多