我从源码中找了一个例子 BuildUpdate
import ( "database/sql" _ "github.com/go-sql-driver/mysql" qb "github.com/didi/gendry/builder" )
#### `BuildUpdate` sign: `BuildUpdate(table string, where map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)` BuildUpdate is very likely to BuildSelect but it **doesn't support**: * _orderby * _groupby * _limit * _having ``` go where := map[string]interface{}{ "foo <>": "aha", "bar <=": 45, "sex in": []interface{}{"girl", "boy"}, } update := map[string]interface{}{ "role": "primaryschoolstudent", "rank": 5, } cond,vals,err := qb.BuildUpdate("table_name", where, update) db.Exec(cond, vals...)
// 查询条件的封装
where := map[string]interface{}{ "foo <>": "aha", "bar <=": 45, "sex in": []interface{}{"girl", "boy"}, }
// 更新的数据
update := map[string]interface{}{ "role": "primaryschoolstudent", "rank": 5, }
// 函数传入 数据表名 查询条件 新数据 生成操作
cond,vals,err := qb.BuildUpdate("table_name", where, update) // 执行更新操作 db.Exec(cond, vals...)