I am trying to retrieve a DATETIME field called TIMESTAMP from an SQLite DB with the following code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
var d1: NSDate = NSDate()
if getData.dictLocations["TIMESTAMP"] != nil {
d1 = dateFormatter.dateFromString(getData.dictLocationsNames["TIMESTAMP"]!)!
} else {
dateCreatedLabel.text = "FAILED"
}
The problem seems to stem from the fact that there are only two options for getData:
getData.dictLocation which is <String, Double> or
getData.dictLocationNames which is <String, String>
So how do I pull a date? Needless to say the sample code always displays, "FAILED".
Any ideas? I am using FMDB.
This replaces a similar question I asked earlier today, now deleted, that was too broad.
When you pass NSDate
object as parameter to executeQuery
or executeUpdate
in FMDB, it does the necessary conversion for you. You don't have to (and shouldn't) use NSDateFormatter
at all. Let FMDB handle that for you.
For example:
if !db.executeUpdate("create table foo (bar text, transactionTimestamp text)", withArgumentsInArray: nil) {
println(db.lastErrorMessage())
}
if !db.executeUpdate("insert into foo (bar, transactionTimestamp) values (?, ?)", withArgumentsInArray: ["baz", NSDate()]) {
println(db.lastErrorMessage())
}
if let rs = db.executeQuery("select bar, transactionTimestamp from foo", withArgumentsInArray: nil) {
while rs.next() {
let bar = rs.stringForColumn("bar")
let timestamp = rs.dateForColumn("transactionTimestamp")
println("bar = \(bar); timestamp = \(timestamp)")
}
rs.close()
} else {
println(db.lastErrorMessage())
}