gpt4 book ai didi

iOS。 xcode 在文档目录/开始时复制一个零字节的 sqlite 数据库

转载 作者:行者123 更新时间:2023-11-30 10:59:11 25 4
gpt4 key购买 nike

编辑:

按照 Maddy 的要求,我添加了“打开”文件的代码:

我删除了一个应用程序并通过 Xcode 再次运行它。在文档目录中没有文件之前,我检查了,由于没有文件,我可以从主包中复制 32 字节的 sqlite 文件,其中包含我的表。

问题是,现在我删除它后,如果文档目录中没有文件,它运行时会在目录文件夹中创建一个零字节有效的 sqlite 文件,其中没有表。因此,在 appdelegate 中,当我检查文件时,它正确地说存在该文件,但不是我想要的文件。如果我手动转到设备中的 iTunes 并删除该文件,它会复制正确的文件。

这在模拟器和设备中都会发生。任何想法我可能做错了什么。请在下面找到一段来自应用程序委托(delegate)的代码: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
}
}

并且它有效。非常感谢您提供的线索。

关于iOS。 xcode 在文档目录/开始时复制一个零字节的 sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53604215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com