I'm building an IOS swift app, i've found the need of storing some data in sqlite. I'm using FMDB with objc bridging-header. Everything works fine, no errors show up and the data goes in to the db. But when i shut the app and reload it again the db is empty once more.
func insertUserInLocalDB(userToAdd : User) -> Bool{
if myDatabase.open() {
var identifier = userToAdd.userId
var userName = userToAdd.name
var nickname = userToAdd.nickname
var city = userToAdd.city
var nationality = userToAdd.nationality
var gender = userToAdd.gender
var datebirth = userToAdd.datebirth
var phone = userToAdd.phone
var photoAvatar = userToAdd.photoAvatar
if !userToAdd.userId.isEmpty { identifier = userToAdd.userId }else{ identifier = "" }
if !userToAdd.name!.isEmpty { userName = userToAdd.name! }else{ userName = "" }
if !userToAdd.nickname!.isEmpty { nickname = userToAdd.nickname! }else{ nickname = "" }
if userToAdd.city != nil { city = userToAdd.city! }else{ city = "" }
if userToAdd.nationality != nil { nationality = userToAdd.nationality! }else{ nationality = "" }
if userToAdd.gender != nil{ gender = userToAdd.gender! }else{ gender = "" }
if userToAdd.datebirth != nil { datebirth = userToAdd.datebirth! }else{ datebirth = NSDate() }
if userToAdd.phone != nil { phone = userToAdd.phone! }else{ phone = "" }
if userToAdd.photoAvatar != nil { photoAvatar = userToAdd.photoAvatar! }else{ photoAvatar = "" }
let insertSQL = "INSERT INTO Users (identifier,name,nickname,city,nationality,gender,datebirth,phone,photoAvatar) VALUES ('\(identifier)','\(userName!)','\(nickname!)','\(city!)','\(nationality!)','\(gender!)','\(datebirth!)','\(phone!)','\(photoAvatar!)')"
let result = myDatabase.executeUpdate(insertSQL,
withArgumentsInArray: nil)
if !result {
println("Failed to add contact")
println("Error: \(myDatabase.lastErrorMessage())")
} else {
println("Contact Added")
}
myDatabase.close()
return true
}
return false
}
I'm really losing it with this issue, i've been around this for a week now...
Edit1:
I'm loading the file like this:
init(databasePath:String){
self.databasePath = databasePath
let path = NSBundle.mainBundle().pathForResource(databasePath, ofType:"sqlite")
self.myDatabase = FMDatabase(path: path)
}
Aucun commentaire:
Enregistrer un commentaire