Swift 2 example
A few assumptions:
- You added
libsqlite3.tbd to your project correctly. - You have correctly added the sqlite database to your project.
- You copied the required FMDB files to your project correctly.
- You have a ViewController named
ViewController.swift
In Xcode, open the ViewController.swift file and find the following code:
override func viewDidLoad() { super.viewDidLoad()
Add the following code below the comment and remember to change the database name on lines 4 and 5.
// Start of Database copy from Bundle to App Document Directory let fileManager = NSFileManager.defaultManager() let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]) let destinationSqliteURL = documentsPath.URLByAppendingPathComponent("sqlite.db") let sourceSqliteURL = NSBundle.mainBundle().URLForResource("sqlite", withExtension: "db") if !fileManager.fileExistsAtPath(destinationSqliteURL.path!) { // var error:NSError? = nil do { try fileManager.copyItemAtURL(sourceSqliteURL!, toURL: destinationSqliteURL) print("Copied") print(destinationSqliteURL.path) } catch let error as NSError { print("Unable to create database \(error.debugDescription)") } } // Let print the path to the database on your Mac // so you can go look for the database in the Finder. print(documentsPath) let db = FMDatabase(path: destinationSqliteURL.path) // Let open the database if !db.open() { print("Unable to open database") return } // Let run some SQL from the FMDB documents to make sure // everything is working as expected. if !db.executeUpdate("create table test(x text, y text, z text)", withArgumentsInArray: nil) { print("create table failed: \(db.lastErrorMessage())") } if !db.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", withArgumentsInArray: ["a", "b", "c"]) { print("insert 1 table failed: \(db.lastErrorMessage())") } if !db.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", withArgumentsInArray: ["e", "f", "g"]) { print("insert 2 table failed: \(db.lastErrorMessage())") } if let rs = db.executeQuery("select x, y, z from test", withArgumentsInArray: nil) { while rs.next() { let x = rs.stringForColumn("x") let y = rs.stringForColumn("y") let z = rs.stringForColumn("z") print("x = \(x); y = \(y); z = \(z)") } } else { print("select failed: \(db.lastErrorMessage())") } db.close()
Now you can run the project and copy your database.
Previous answer if it helps someone
The documentation for FMDB is pretty useful on this topic. Using the example here, the code below should work, assuming the following:
- You are using
Swift 1.2 or later. - You have correctly added
FMDB to your project. - An SQLite database called
contacts.db been added to the project. In Build Phases , then Link Binary With Libraries , which you added libsqlite3.dylib .
// // ViewController.swift // import UIKit class ViewController: UIViewController { @IBOutlet weak var name: UITextField! @IBOutlet weak var address: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let filemgr = NSFileManager.defaultManager() let documentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String let path = documentsFolder.stringByAppendingPathComponent("contacts.db") let database = FMDatabase(path: path) if !database.open() { println("Unable to open database") return } if !database.executeUpdate("create table test(x text, y text, z text)", withArgumentsInArray: nil) { println("create table failed: \(database.lastErrorMessage())") } if !database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", withArgumentsInArray: ["a", "b", "c"]) { println("insert 1 table failed: \(database.lastErrorMessage())") } if !database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", withArgumentsInArray: ["e", "f", "g"]) { println("insert 2 table failed: \(database.lastErrorMessage())") } if let rs = database.executeQuery("select x, y, z from test", withArgumentsInArray: nil) { while rs.next() { let x = rs.stringForColumn("x") let y = rs.stringForColumn("y") let z = rs.stringForColumn("z") println("x = \(x); y = \(y); z = \(z)") } } else { println("select failed: \(database.lastErrorMessage())") } database.close() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
To check, just use the Build and Run button, and you should see the simulator launch and the following results in the Xcode console:
x = a; y = b; z = c x = e; y = f; z = g
To display the console, enter Shift + CMD + R or go to the View menu β Debug Area β Activate Console