首页 > 解决方案 > 我想将我的 Sqlite 数据库文件直接添加到 iOS 项目

问题描述

我想在 iOS 应用程序中手动放置 Sqlite 数据库,我想对此执行操作。我不想通过编程将我的 Db 文件复制到任何默认设备文件夹。

唯一要求我想要一个数据库,它不应该在设备文件夹中可用。

我看到了一些示例代码片段

以下代码将我的手动数据库复制到设备默认目录,但它不应该这样做。我的数据库文件不应该在设备中可用。

// 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)")
}

标签: iosswift

解决方案


推荐阅读