duanke0178 2017-11-19 08:38
浏览 73
已采纳

如何在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?

  • 写回答

1条回答 默认 最新

  • drn1008 2017-11-19 08:44
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?