iOS。 xcode 在文档目录/开始时复制一个零字节的 sqlite 数据库/
iOS. xcode copies a zero byte sqlite database at start in the documents directory/
编辑:
正如 Maddy 所问,我包含 "opens" 文件的代码:
我删除了一个应用程序并通过 Xcode 再次 运行 它。在文档目录中没有文件之前,我检查了一下,因为没有文件,我可以从主包中复制我的 32 字节 sqlite 文件和我的表。
问题是,现在在我删除它之后,它会在目录文件夹中创建一个零字节有效的 sqlite 文件,如果文档目录中没有文件,则 运行s 时其中没有表。因此,在 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
}
}
并且有效。非常感谢您提供线索。
编辑:
正如 Maddy 所问,我包含 "opens" 文件的代码:
我删除了一个应用程序并通过 Xcode 再次 运行 它。在文档目录中没有文件之前,我检查了一下,因为没有文件,我可以从主包中复制我的 32 字节 sqlite 文件和我的表。
问题是,现在在我删除它之后,它会在目录文件夹中创建一个零字节有效的 sqlite 文件,如果文档目录中没有文件,则 运行s 时其中没有表。因此,在 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
}
}
并且有效。非常感谢您提供线索。