I am using FMDB wrapper for my database.I can fetch data using FMResultSet,but when I am trying to return FMResultSet to another ViewController,it returns nil.I am calling my database from here
var resultSet: FMResultSet! = db.getUserById(1)
if(resultSet != nil) {
self.setUserInfo(resultSet)
}
and here is my database coding part
func getUserById(userId: Int) -> FMResultSet {
let sharedInstance = DatabaseHandler()
var database: FMDatabase? = nil
var resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM user_info WHERE user_id = ?", withArgumentsInArray: [userId])
if(resultSet != nil) {
while resultSet.next() {
var name: String = "USER_NAME"
var location = "USER_LOCATION"
println("Name: \(resultSet.stringForColumn(name))")
println("Location: \(resultSet.stringForColumn(location))")
}
}
sharedInstance.database!.close()
return resultSet
}
When I am printing those values,it shows the values in console,but when I am returning the resultSet,it appears to be nil
What have I done worng?
Your resultSet is only valid while the database is open. Return something other than the resultSet (i.e., a wrapper object/dictionary/whatever). Generally you iterate the resultSet and pull out what you need and use that. Alternately you hand out the resultSet to the caller and it calls .next
and closes it when done.