ios - IOS。xcode 在文件目录/开始时复制一个零字节的 sqlite 数据库
问题描述
编辑:
正如 Maddy 所问的,我包括“打开”文件的代码:
我删除了一个应用程序并通过 Xcode 再次运行它。在文档目录中没有文件之前,所以我检查了并且由于没有文件,我可以从主包中复制包含我的表的 32 字节 sqlite 文件。
问题是,现在在我删除它之后,如果文档目录中没有文件,它会在运行时在目录文件夹中创建一个零字节有效的 sqlite 文件,其中没有表。因此,在 appdelegate 中,当我检查文件时,它说正确,但不是我想要的文件。如果我手动转到设备中的 iTunes 并删除文件,它会复制正确的文件。
这在模拟器和设备中都会发生。任何想法我会做错什么。请在下面找到来自应用程序委托的一段代码:Xcode 10.1 和 swift 4.2 语法:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Fabric.with([Crashlytics.self])
let sourcePath = Bundle.main.path(forResource: "mydb", ofType: "db")!
let fileManager = FileManager.default
let doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let destinationPath = doumentDirectoryPath.appendingPathComponent("mydb")
if !fileManager.fileExists(atPath: destinationPath) {
do {
try fileManager.copyItem(atPath: sourcePath, toPath: destinationPath)
}
catch
{
os_log("error copying database")
Crashlytics.sharedInstance().recordError(error)
print(error)
}
}
return true
}
打开文件的代码
import Foundation
import SQLite
import os.log
var connected: Bool
//MARK: - private attributes
var db: Connection?
//MARK: - constructor
init() {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
do {
db = try Connection("\(path)/mydb.db")
connected = true
}
catch {
connected = false
}
}
提前谢谢了。这真的让我很困惑。
解决方案
我正在使用 Sqlite.swift,这是该Connection
方法的构造函数:
/// Initializes a new SQLite connection.
///
/// - Parameters:
///
/// - location: The location of the database. Creates a new database if it
/// doesn’t already exist (unless in read-only mode).
///
/// Default: `.inMemory`.
///
/// - readonly: Whether or not to open the database in a read-only state.
///
/// Default: `false`.
///
/// - Returns: A new database connection.
public init(_ location: SQLite.Connection.Location = default, readonly: Bool = default) throws
因此,如果不存在,它会创建数据库(没有表的空白)。所以我将已编辑问题中的代码更改为:
//MARK: - constructor
init() {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
do {
let sourcePath = Bundle.main.path(forResource: "mydb", ofType: "db")!
let fileManager = FileManager.default
let doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let destinationPath = doumentDirectoryPath.appendingPathComponent("mydb.db")
if !fileManager.fileExists(atPath: destinationPath) {
try fileManager.copyItem(atPath: sourcePath, toPath: destinationPath)
}
db = try Connection("\(path)/mydb.db")
connected = true
}
catch {
connected = false
}
}
它有效。非常感谢提供线索。
推荐阅读
- json - 如何在画布上呈现 json 数据?
- android - Android协程处理异常
- uwp - UWP TreeView如何允许删除特定项目
- r - 固定利率衰减的累积存款
- apache-spark - 使用 delta 格式使用 Apache Spark 创建表被卡住了
- c++ - Clion 无法解析模板
- unit-testing - 如何在 Hyperledger Composer 中通过单元测试引用一系列产品
- java - Spring中声明性事务中的传播(选择正确)问题
- node.js - 如何使用节点 js 更新 redis 缓存
- scroll - 如何从 div 元素中分离水平滚动条并将其放入另一个水平位置 - react-table 案例?