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())")
}
}
}