Update

6.Update

You can use Update to update the record. The first param of this method is the pointer of struct or map[string]interface{} which need to be update. When this param is the pointer of struct, only non-empty and non-zero field will be updated to database. When it is a map[string]interface{}, the map key is the name of the column will be updated, the map value is the content needs to be updated. Update method will return two parameters. First one is the affected number. Please take notice, SQLITE will only return the count of update conditions not the real affected records.

user := new(User)
user.Name = "myname"
affected, err := engine.ID(id).Update(user)

But if you want to update a zero value to database. There are three chosen:

  1. Use Cols to indicate the columns will need to be update even if it is zero or empty.
affected, err := engine.ID(id).Cols("age").Update(&user)
  1. Use AllCols to indicate that every columns will be updated even if it is zero or empty.
affected, err := engine.ID(id).AllCols().Update(&user)
  1. Use map[string]interface{}, but you need specify the table via a pointer of struct or a string.
affected, err := engine.Table(new(User)).ID(id).Update(map[string]interface{}{"age":0})
affected, err := engine.Table("user").ID(id).Update(map[string]interface{}{"age":0})