如何在sqlx中增加字段值

In a Go app, using sqlx package and mysql database, I want to update table user and increment its posts field by 1:

err = shared.Dbmap.Exec("UPDATE user SET posts=posts+1 WHERE id=?", userId)
if err != nil {
    log.Println(err)
    return
}

However at compile time I get:

multiple-value shared.Dbmap.DB.Exec() in single-value context

I looked at the docs and could not see relevant examples.

How can I fix it?

This is how to read the message multiple-value shared.Dbmap.DB.Exec() in single-value context:

  • shared.Dbmap.DB.Exec() has multiple values
  • You are trying to use it in a single-value context

In your code you have err = shared.Dbmap.Exec(...).

On the left side of the assignment there is a single value, on the right side there are multiple.

Looking at the docs, the Exec(...) function returns 2 values, but you assign it to one value.

Write like this:

_, err = shared.Dbmap.Exec("UPDATE user SET posts=posts+1 WHERE id=?", userId)