启动swift后复制数据库 [英] Copy Database after starting swift

查看:26
本文介绍了启动swift后复制数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个数据库项目,所以我面临一些问题.希望你能帮助我!我正在使用 FMDB 来访问现有数据库.当我尝试执行像选择 * 从电影"这样的简单查询时,它会返回没有这样的表"之类的东西.我查看了 iPhone 模拟器的文件夹并找到了数据库,但它是空的.我的下一步是,包括这个方法:

this is my first DB Project, so i am facing some issues. Hopefully you can help me! I am using FMDB to have access to an existing DB. When i try to execute a simpel Query like "Select * From films" it returns stuff like "no such table". I looked into the folder of the IPhone Simulator and found the DB, but it was empty. My next Step was, to include this Method:

func copyDatabaseIfNeeded() {
    // Move database file from bundle to documents folder

    let fileManager = FileManager.default

    let documentsUrl = fileManager.urls(for: .documentDirectory,
                                        in: .userDomainMask)

    guard documentsUrl.count != 0 else {
        return // Could not find documents URL
    }

    let finalDatabaseURL = documentsUrl.first!.appendingPathComponent("foo.db")

    if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
        print("DB does not exist in documents folder")

        let documentsURL = Bundle.main.resourceURL?.appendingPathComponent("foo.db")

        do {
            try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalDatabaseURL.path)
        } catch let error as NSError {
            print("Couldn't copy file to final location! Error:\(error.description)")
        }

    } else {
        print("Database file found at path: \(finalDatabaseURL.path)")
    }
}

但是这个方法不起作用.我从 DidFinishLaunching 调用它.

But this Method is not working. Im calling it from DidFinishLaunching.

这是错误信息:

OverBurned/Library/Developer/CoreSimulator/Devices/B5EAE004-A036-4BD5-A692-C25EF3875D25/data/Containers/Bundle/Application/5ABA8D38-7625-4F98-83E9-4266A3E5B6B0/GameOne.app/foo.db, NSUnderlyingError=0x600000053230 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

()

是我使用了错误的方法还是执行错误?

Am i using the wrong Method or is implemented wrong?

推荐答案

错误很明显.您的应用程序的资源包中没有 foo.db.

The error is clear. There is no foo.db in your app's resource bundle.

您发布的代码确实存在很多问题.

You do have lots of issues with the code you posted.

  1. 您获取 foo.db 路径的代码远非理想.
  2. 您没有正确处理可选项.
  3. 您的变量名称需要改进.示例 - 第二个 documentsURL 暗示它是一个引用Documents"文件夹的 URL.它实际上是资源包中文件的URL.
  4. 不需要NSError.
  1. Your code to get the path to foo.db is far from ideal.
  2. You do not properly deal with optionals.
  3. Your variable names need to be improved. Example - the 2nd documentsURL implies it is a URL referencing the "Documents" folder. It is actually a URL to a file in the resource bundle.
  4. There is no need for NSError.

以下是我将如何编写此代码:

Here is how I would write this code:

func copyDatabaseIfNeeded() {
    // Move database file from bundle to documents folder

    let fileManager = FileManager.default

    guard let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

    let finalDatabaseURL = documentsUrl.appendingPathComponent("foo.db")

    do {
        if !fileManager.fileExists(atPath: finalDatabaseURL.path) {
            print("DB does not exist in documents folder")

            if let dbFilePath = Bundle.main.path(forResource: "foo", ofType: "db") {
                try fileManager.copyItem(atPath: dbFilePath, toPath: finalDatabaseURL.path)
            } else {
                print("Uh oh - foo.db is not in the app bundle")
            }
        } else {
            print("Database file found at path: \(finalDatabaseURL.path)")
        }
    } catch {
        print("Unable to copy foo.db: \(error)")
    }
}

这篇关于启动swift后复制数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆