Clifton Labrum
Clifton Labrum

Reputation: 14070

Dynamically UPDATE Variable Number of Columns with FMDB in Swift

Let's say I have an array of columns and an array of values like this:

let columns = ["a = ?","b = ?","c = ?"]
let values = ["one","two","three","entryId100"]

I have a table in my SQLite database with columns named "a", "b", and "c".

If I try to do this:

let columnsString = columns.joinWithSeparator(", ")
let sql = "UPDATE table SET \(columnsString) WHERE entryId = ?"
db.executeUpdate(sql, withArgumentsInArray:[values])

I get an error that the bind count (1) is not correct for the # of variables in the query (4).

But if I print my sql string, it looks good:

UPDATE table SET a = ?, b = ?, c = ? WHERE entry = ?

It seems to not be recognizing my dynamically generated column = ? values. Is there any way I can do that?

Upvotes: 0

Views: 927

Answers (1)

Gwendal Roué
Gwendal Roué

Reputation: 4044

It's db.executeUpdate(sql, withArgumentsInArray:values), not db.executeUpdate(sql, withArgumentsInArray:[values]) (don't wrap values in an array)

Upvotes: 1

Related Questions