-
Notifications
You must be signed in to change notification settings - Fork 9
Update方法
Tuuz edited this page Apr 2, 2026
·
6 revisions
func Api_update_img(id, img interface{}) bool {
db := tuuz.Db().Table(Table)
where := map[string]interface{}{
"id": id,
}
db.Where(where)
data := map[string]interface{}{
"img": img,
}
db.Data(data)
_, err := db.Update()
if err != nil {
Log.Dbrr(err, tuuz.FUNCTION_ALL())
return false
} else {
return true
}
}这里外部调用的方法参考Insert部分,记得调用的时候要注入Db连接,不然Db就是nil的状态,你也可以在方法里面加入一个没有传入就自动创建的判断,但是不建议,因为虽然这样看似不会出错,但是如果在项目后期这些调用方法被遗忘,则有可能出现需要注入Db的地方忘记注入导致脏读脏写脏更新后患无穷,所以这里建议不要直接写,养成良好习惯并且在编程测试阶段就把这类问题干掉,降低后期需要debug自己记忆的难度是后期也能继续顺利推进项目的关键!
func (self *Interface) Api_update_img(id, parent_id, img interface{}) bool {
db := self.Db.Table(Table)
where := map[string]interface{}{
"id": id,
"parent_id": parent_id,
}
db.Where(where)
data := map[string]interface{}{
"img": img,
}
db.Data(data)
_, err := db.Update()
if err != nil {
Log.Dbrr(err, tuuz.FUNCTION_ALL())
return false
} else {
return true
}
}