ios - Swift iOS 文件管理器复制更改 sqlite 文件

标签 ios sqlite nsfilemanager

我有一个例程,当我的 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/

相关文章:

ios - 将两个 iOS 项目合并为一个

ios - 应用内购买是否适用于电子书/有声读物?

php - PDO准备语句来存储html内容

sqlite - 使用 clojure 的 korma sqlite3 助手时,sqlite3 数据库的默认路径是什么?

ios - NSArray writeFileToPath 失败

ios - 如何将现有对象添加到 Parse.com Databrowser 中的关系中

ios - NSInvalidArgumentException : Workout_Tracker. QuickAddViewController collectionView:numberOfItemsInSection:]:无法识别的选择器发送到实例

sqlite - 如何将SQLite数据库连接到Weka

swift - 使用 FileManager 复制文件时出错(CFURLCopyResourcePropertyForKey 失败,因为它传递了一个没有方案的 URL)

objective-c - 苹果操作系统 : how to determine path is file or directory