I am trying to include a pre-populated SQLite database (with FMDB wrapper) in a build to my physical iPhone. However, the pre-populated database is not being included in the build to the physical device.
Note that it works fine in the IOS Simulator, but only for one simulator device.
I have included the database in Build Phases > Copy Bundled Resources and linked libsqlite3.dylib under Link Binary with Libraries.
What Swift code do I need to add to get the database included in the build to the physical device?
Code:
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.
}
}
UPDATE - Working Swift 2 code in XCode 7 - Pre-populated SQLite database using FMDB wrapper copied to the physical device 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.
}
}