ios - 我想将我的 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)")
}
解决方案
推荐阅读
- macos - WKWebView takeSnapshot 没有滚动条 macOS
- console - 无法从 Web 浏览器访问 Websphere 控制台
- twilio - 在没有 flex 的情况下集成 Twilio 组件
- ruby - 在登录 xfce 时运行 ruby 脚本
- java - Java - 使用 JSch 但尝试在 FTP 上移动文件时出错 - %RNFR-bad%
- html - 如何获得背景仅跨越内容并保持间距的内联行
- flutter - 将值传递给前一个小部件
- postman - 空手道 API:使用 PUT 方法发送请求时出现“405 Method Not Allowed”错误
- shopify - 在 Shopify 的产品页面上添加两个动态结帐按钮?
- javascript - 关闭按钮在弹出窗口上不起作用