I'm trying to create an app with login/signup. I'm using sqlite to save the user information and password. But it's not working since the database isn't being loaded into the code.
let filemgr = NSFileManager.defaultManager()
let dirPaths =
NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
.UserDomainMask, true)
var docsDir = dirPaths[0] as! String
print(docsDir)
var databasePath = docsDir + "/contacts.sqlite"
if filemgr.fileExistsAtPath(databasePath as String) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB == nil {
print("Error: \(contactDB.lastErrorMessage())")
}
//Remaining code
}
I inserted print (contactDB)
right below it's declaration to see what it contains.
2016-02-24 13:27:59.445 Login[12850:1625864] The FMDatabase <FMDatabase: 0x7fd76b804750> is not open.
nil
This is what I got in the console. "Login" is the class name where this code is from. I'm unable to make progress since I'm unable to load the database. Any help is much appreciated. Thanks in advance!
EDIT: The problem was resolved after I added contactDB.open(). I'm still having problem with it.
<FMDatabase: 0x7fbb41f5fa70>
Optional(false)
2016-02-24 14:18:51.272 Login[13064:1649879] Error calling sqlite3_step (21: out of memory) rs
I'm getting this in the console when I try to print the error.
It says it's not open. Try contactDB.open()
.
or
if contactDB.open() {
// Do your DB work here.
contactDB.close()
} else {
println("Error: \(contactDB.lastErrorMessage())")
}