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.

  1. user := new(User)
  2. user.Name = "myname"
  3. 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.
  1. 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.
  1. 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.
  1. affected, err := engine.Table(new(User)).ID(id).Update(map[string]interface{}{"age":0})
  2. affected, err := engine.Table("user").ID(id).Update(map[string]interface{}{"age":0})