I have a TEST table that appears to be behaving just weird. It simply consists of two integers, ID (auto increment) and LOCATIONSELECTED.
The table is populated
1, 99
2, 100
3, 101
etc
If I try and find LOCATIONSELECTED using the code below it works and returns 99!
func getTestSelectedRecord() {
sharedInstance.database!.open()
var sqlStatement = "SELECT * FROM TEST WHERE ID = 1"
var resultSet: FMResultSet! = sharedInstance.database!.executeQuery(sqlStatement, withArgumentsInArray: nil)
if (resultSet != nil) {
while resultSet.next() {
let getRecordID = resultSet.intForColumn("LOCATIONSELECTED")
NSLog("DatabaseFunctions - TEST - GET - getRecordID = %i", getRecordID)
}
}
sharedInstance.database!.close()
}
But if I remove the while loop it returns nul. I normally write my SELECT calls without the if != nil and while calls at all. There is only one record with an ID of 1 in the table.
I presume something subtle is going on here. Can anyone explain what it is?
You can remove the while loop, but you must maintain the:
resultSet.next();
statement, since is this the operation that allows to access the first (and only) record of the result.