gorp更新不起作用

huangapple go评论127阅读模式
英文:

gorp Update not updating

问题

我在使用gorp更新postgresql数据库中的一行时遇到了问题。我能够成功地使用db.Exec运行更新操作,所有列都会更新为正确的信息,但是使用gorp时,我只能更新非sql.Null*字段,而其他字段保持不变。

这段代码中,db.Exec的更新操作是有效的:

  1. db, _ := getDB()
  2. db.Exec("UPDATE network SET ssid=$1,lat=$2,lon=$3,sec=$4,channel=$5,found=$6,datefirst=$7,datelast=$8,bssid=$9 WHERE id=$10",
  3. npr.Ssid, npr.Lat.Float64, npr.Lon.Float64, npr.Sec.String, npr.Channel.Int64, npr.Found, npr.Datefirst.String, npr.Datelast.String, npr.Bssid.String, getNetworkId(ssid))

而这段代码中的dbmap.Update操作则无效:

  1. func updateNetwork(n *WirelessNetwork) {
  2. _, dbmap := getDB()
  3. _, err := dbmap.Update(n)
  4. if err != nil {
  5. log.Fatal("updateNetwork - ", err)
  6. }
  7. }

希望这能帮到你。

英文:

Im having issues updating a row in my postgresql database with gorp, im successfully able run the update using db.Exec, all columns get updated with the right information, while with gorp im only able to update the non sql.Null* fields while the rest remain unchanged.

  1. var db *sql.DB
  2. var dbmap *gorp.DbMap
  3. func getDB() (*sql.DB, *gorp.DbMap) {
  4. if db == nil {
  5. var err error
  6. db, err = sql.Open("postgres", "postgres://xxxxxxxx")
  7. db.SetMaxOpenConns(5)
  8. db.SetMaxIdleConns(0)
  9. dbmap = &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
  10. dbmap.AddTableWithName(WirelessNetwork{}, "network").SetKeys(true, "Id")
  11. if err != nil {
  12. log.Panic(err)
  13. }
  14. }
  15. return db, dbmap
  16. }
  17. type WirelessNetwork struct {
  18. Id int `db:"id"`
  19. Ssid string `db:"ssid"`
  20. Lat sql.NullFloat64 `db:"lat"`
  21. Lon sql.NullFloat64 `db:"lon"`
  22. Sec sql.NullString `db:"sec"`
  23. Bssid sql.NullString `db:"bssid"`
  24. Channel sql.NullInt64 `db:"channel"`
  25. Found bool `db:"found"`
  26. Datefirst sql.NullString `db:"datefirst"`
  27. Datelast sql.NullString `db:"datelast"`
  28. }
  29. npr := new(WirelessNetwork)
  30. npr.Id = getNetworkId(ssid)
  31. npr.Ssid = ssid
  32. npr.Lat = dbProbes[index].Lat
  33. npr.Lon = dbProbes[index].Lon
  34. npr.Sec = dbProbes[index].Sec
  35. npr.Bssid = dbProbes[index].Bssid
  36. npr.Channel = dbProbes[index].Channel
  37. npr.Found = dbProbes[index].Found
  38. npr.Datefirst = dbProbes[index].Datefirst
  39. npr.Datelast = dbProbes[index].Datelast
  40. npr.Found = true

This works

  1. db, _ := getDB()
  2. db.Exec("UPDATE network SET ssid=$1,lat=$2,lon=$3,sec=$4,channel=$5,found=$6,datefirst=$7,datelast=$8,bssid=$9 WHERE id=$10",
  3. npr.Ssid, npr.Lat.Float64, npr.Lon.Float64, npr.Sec.String, npr.Channel.Int64, npr.Found, npr.Datefirst.String, npr.Datelast.String, npr.Bssid.String, getNetworkId(ssid))

This does not

  1. func updateNetwork(n *WirelessNetwork) {
  2. _, dbmap := getDB()
  3. _, err := dbmap.Update(n)
  4. if err != nil {
  5. log.Fatal("updateNetwork - ", err)
  6. }
  7. }

答案1

得分: 6

sql.Null* 类型是带有 Valid 布尔字段的结构体,用于指示值是否为 NULL。布尔字段的初始值为 false,所以除非你明确验证数据,否则会将 NULL 值发送到数据库。你没有告诉我们 dbProbes 是什么以及它如何获取数据,但如果它是通过以下方式初始化的:

  1. dbProbes[index].Lat = sql.NullFloat64{Float64: lat}

那么 Valid 仍然为 false,你需要手动验证数据:

  1. dbProbes[index].Lat = sql.NullFloat64{Float64: lat, Valid: true}

或者使用 Scan 方法:

  1. err = dbProbes[index].Lat.Scan(lat)
英文:

sql.Null* types are structs with Valid boolean field, which tells if the value is NULL. The initial value for boolean is false, so unless you explicitly validate your data, you'll be sending NULLs to the database. You didn't tell us, what is dbProbes and how does it get the data, but if it's initialised with something like

  1. dbProbes[index].Lat = sql.NullFloat64{Float64: lat}

then Valid is still false, and you need to either validate your data manually:

  1. dbProbes[index].Lat = sql.NullFloat64{Float64: lat, Valid: true}

or use the Scan method:

  1. err = dbProbes[index].Lat.Scan(lat)

huangapple
  • 本文由 发表于 2015年6月8日 16:03:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/30703965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定