1

I am following a tutorial on using SQLite on Swift, everything is working fine (the code is below). I am confusing about the database itself. I would be grateful if an expert could clarify the following questions to me:
1- I want the database to be part of the app to be used offline I mean the user will download the app with the database inside, where should I copy the database itself?
2- I tried to copy the database to supporting files folder but when the app runs in my iphone the database is not copied to my iphone. Instead of I have to build a new database in the iphone
3- The database will be large so I don't want to build it in the app (I am planning to copy data from excel using Firefox SQLite Manager, alternatives for copying are very welcome). Any hints are more than welcome. thanks in advance.

let filemgr = NSFileManager.defaultManager()
    let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

    let docsDir = dirPaths [0] as! String

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

    if !filemgr.fileExistsAtPath(databasePath as String) {
        let contactDB = FMDatabase (path: databasePath as String)
        if contactDB == nil {println("Error: \(contactDB.lastErrorMessage())")
    }

        if contactDB.open() {
            let sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)"
            if !contactDB.executeStatements(sql_stmt){
                println("Error: \(contactDB.lastErrorMessage())")
            }
            contactDB.close()
        }else {
            println("Error: \(contactDB.lastErrorMessage())")
        }
    }
}

2 Answers 2

4

create your own database and put the database into your xcode project(just drag the database into xcode project) and use the following code to copy the database into your app

 override func viewDidLoad() {
    super.viewDidLoad()

    var fileManager = NSFileManager.defaultManager()
    var Sourcepath = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("DataBase.db");
    let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
    let databaseStr = "DataBase.db"
    let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)
    //check for database existance if not exsit than copy the database destination path
    if(fileManager .fileExistsAtPath(dbPath) == false) {
        var error:NSError?
        fileManager.copyItemAtPath(Sourcepath!, toPath: dbPath, error: &error)
    }
}

once database is copy into application bundle
1) Add libsqlite3.dylib to your project
to add go to application target ->Build Phases ->Link Binary With Libraries and click +

2) Import #import <sqlite3.h> this line into objective-C bridge header file
to create bridge header refer this How to use Objective-C Classes in Swift
know you will be able to access all the sqlite method in you swift code

Sign up to request clarification or add additional context in comments.

9 Comments

Hi where exactly should I insert your code? I am getting some errors
viewDidload() function
OR application didFinishLaunchingWithOptions method
I delete my code and inserted yours, there is no errors, but still can not retrieve the contacts I inserted in the database :(
if u run the app in simulator just add println(dbPath) in my code it will give the database saved path open path and check whether your database actually insert the values or not?
|
0

I think you can not insert a database in Swift, if I recall it is proprietary so you can not edit outside of the app....

1 Comment

maybe is correct on CoreData but I am trying to use FMDB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.