Sqlite.swift: How can I update multiple columns in a row

Created on 4 Oct 2016  路  5Comments  路  Source: stephencelis/SQLite.swift

let alice = users.filter(id == 1)
do {
if try db.run(alice.update(email <- "[email protected]")) > 0 {
print("updated alice")
} else {
print("alice not found")
}
} catch {
print("update failed: (error)")
}

How should I do in order to update many field in a record

Example: I would like to update (email, name, age) of an user with id = 1, But I just found we only can update one field at once

Please Help. Thank you guys so much.

Most helpful comment

@anthonyjosephfarina This throws an "Extra call in argument" error for me, this resolved the error:

let alice = users.filter(id == 1)

try db.run(alice.update(
        [email <- "[email protected]",
        name <- "Alice",
        age <- 24]))

All 5 comments

You create an array of Setters:
var values = [Setter]()

Then you append your Setters as needed and finally execute .update(values).

You can separate each column setter with a comma.

For example:

let alice = users.filter(id == 1)

try db.run(alice.update(
        email <- "[email protected]",
        name <- "Alice",
        age <- 24))

Not a bug, closing.

@anthonyjosephfarina This throws an "Extra call in argument" error for me, this resolved the error:

let alice = users.filter(id == 1)

try db.run(alice.update(
        [email <- "[email protected]",
        name <- "Alice",
        age <- 24]))

@robert-cronin
Thanks for the information!
the [] is SUPER important and saved my day!
I did not notice the difference at first.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pgawlowski picture pgawlowski  路  6Comments

zackdh9 picture zackdh9  路  5Comments

lucascorrea picture lucascorrea  路  5Comments

kartikthapar picture kartikthapar  路  4Comments

bitflying picture bitflying  路  5Comments