gpt4 book ai didi

Swift - 设备上不包含预填充的 SQLite 数据库

转载 作者:IT王子 更新时间:2023-10-29 06:26:12 26 4
gpt4 key购买 nike

我正在尝试在我的物理 iPhone 的构建中包含一个预填充的 SQLite 数据库(带有 FMDB 包装器)。但是,预填充的数据库未包含在物理设备的构建中。

请注意,它在 IOS 模拟器中运行良好,但仅适用于一个模拟器设备。

我已将数据库包含在 Build Phases > Copy Bundled Resources 中,并在 Link Binary with Libraries 下链接了 libsqlite3.dylib。

我需要添加哪些 Swift 代码才能将构建中包含的数据库获取到物理设备?

代码:

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var checkButton: UIButton!
@IBOutlet weak var tests_scroller: UITextView!

var databasePath = NSString()

override func viewDidLoad() {
super.viewDidLoad()

checkButton.setTitle("\u{2610}", forState: .Normal)
checkButton.setTitle("\u{2611}", forState: .Selected)


let filemgr = NSFileManager.defaultManager()

let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

let docsDir = dirPaths[0] as! String

databasePath = docsDir.stringByAppendingPathComponent("vmd_db.db")

let myDatabase = FMDatabase(path: databasePath as String)


if myDatabase.open(){

var arrayData:[String] = []

let query_lab_test = "SELECT lab_test FROM lab_test ORDER BY lab_test ASC"

let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test, withArgumentsInArray: nil)

while results_lab_test?.next() == true {

if let resultString = results_lab_test?.stringForColumn("lab_test"){

arrayData.append(resultString)

}
}
var multiLineString = join("\n", arrayData)
tests_scroller.text = multiLineString
myDatabase.close()
}
}




override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

更新 - 在 XCode 7 中使用 Swift 2 代码 - 使用复制到物理设备的 FMDB 包装器预填充 SQLite 数据库 OK:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var tests_scroller: UITextView!

var databasePath = NSString()

override func viewDidLoad() {
super.viewDidLoad()


let sourcePath = NSBundle.mainBundle().pathForResource("vmd_db", ofType: "db")

let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!

let destinationPath = (documentDirectoryPath as NSString).stringByAppendingPathComponent("vmd_db.db")

do {
try NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath)

} catch _ {
}

//read it
let myDatabase = FMDatabase(path: destinationPath as String)

if myDatabase.open(){

var arrayData:[String] = []

let query_lab_test = "SELECT lab_test FROM lab_test ORDER BY lab_test ASC"

let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test, withArgumentsInArray: nil)

while results_lab_test?.next() == true {

if let resultString = results_lab_test?.stringForColumn("lab_test"){

arrayData.append(resultString)

}
}

let multiLineString = arrayData.joinWithSeparator("\n")

tests_scroller.text = multiLineString
myDatabase.close()
}
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

据我所知,它不应该在任何模拟器/设备上运行,因为您访问的是文档文件夹中的文件。那不在您的 bundle 中。

您需要做的是将文件从 bundle 复制到文档目录,然后再尝试在那里打开它:

//path in documents
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsDir = dirPaths[0]
databasePath = docsDir.stringByAppendingPathComponent("vmd_db.db")

//if not there, copy from bundle
if !filemgr.fileExistsAtPath(databasePath) {
let bundleDatabasePath = NSBundle.mainBundle().pathForResource("vm_db", ofType: "db")
filemgr.copyItemAtPath(bundleDatabasePath, toPath: databasePath)
}

//read it
let myDatabase = FMDatabase(path: databasePath as String)

关于Swift - 设备上不包含预填充的 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32622874/

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