Please bear with me this is my first swift project, I'm not yet up to speed with the syntax/language.
I retrieve data from a web service using AlamoFire and parse it using SwiftyJson. Then I want to insert it into a local SQLite database using SQLite.swift.
So far AlomoFire, SwiftyJson and SQLite.swift are installed and working in my project, things went very smooth to my surprise ;)
Now my question. The data I got is in JSON, in an Array so I want to do this:
let companiesTable = db["companies"] // get the table from our SQLite Database
db.transaction(.Deferred) { txn in
for (index,company) in data {
if companiesTable.insert(name <- company["name"]!), shortname <- company["short_name"]).statement.failed {
return .Rollback
}
}
return .Commit
}
My problem is in the insert. I have to force unwrap using a !, which is ok for name (required column in the database), but not ok for shortname or a dozen other columns (not mentioned in the example above for simplicity) which may be empty/null. Of course only columns with a value should be inserted.
Second question. I found the stuff about transactions here on stackoverflow, is the transaction automatically Committed or Roll-backed when doing the 'return'