我试图将一个预先填充的sqlite数据库(带有FMDB包装器)包含在我的物理iPhone的构建中.但是,预先填充的数据库未包含在物理设备的构建中.
请注意,它在IOS模拟器中工作正常,但仅适用于一个模拟器设备.
我已将数据库包含在Build Phases>中复制捆绑资源并将Link Binary下的libsqlite3.dylib与Libraries联系起来.
我需要添加什么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,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. } }
解决方法
从我看到它不应该在任何模拟器/设备上工作,因为您访问文档文件夹中的文件.那不在你的包中.
您需要做的是将文件从包中复制到文档目录,然后尝试在那里打开它:
//path in documents let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。