大佬教程收集整理的这篇文章主要介绍了Swift – 设备上未包含预先填充的SQLite数据库,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
请注意,它在IOS模拟器中工作正常,但仅适用于一个模拟器设备.
我已将数据库包含在Build Phases>中复制捆绑资源并将Link Binary下的libsqlite3.dylib与Libraries联系起来.
我需要添加什么Swift代码才能将构建中包含的数据库添加到物理设备中?
码:
import UIKit class ViewController: UIViewController { @IBOutlet weak var @R_675_10943@kButton: UIButton! @IBOutlet weak var tests_scroller: UITextView! var databasePath = NSString() override func viewDidLoad() { super.viewDidLoad() @R_675_10943@kButton.settitle("\u{2610}",forState: .Normal) @R_675_10943@kButton.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)
以上是大佬教程为你收集整理的Swift – 设备上未包含预先填充的SQLite数据库全部内容,希望文章能够帮你解决Swift – 设备上未包含预先填充的SQLite数据库所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。