dongtaotao19830418 2018-09-25 13:32
浏览 173
已采纳

当数据存在时,Golang Mysql扫描返回零。

Having a bit of a head scratcher moment. I've got the following struct:

type Room struct {
    ID              int
    Name            string
    RoomType        int
    CreatedAt       time.Time
    UpdatedAt       time.Time
    DeletedAt       time.Time
    GroupId         int
    BlockId         int
    ProjectId       int
    RoomLength      float64
    RoomWidth       float64
    CeilingHeight   float64
    CeilingColorHex string
    WallColorHex    string
    FloorColorHex   string
    CeilingColorRgb string
    WallColorRgb    string
    FloorColorRgb   string
}

Reading it out in rest api with:

database := db.New()
stmt, err := database.Prepare("SELECT * FROM room WHERE block_id = ?")
if err != nil {
    panic(err)
}
defer stmt.Close()

rows, err := stmt.Query(c.Param("id"))
if err != nil {
    panic(err)
}
defer rows.Close()

var rooms []common.Room
for rows.Next() {
    var r common.Room
    err := rows.Scan(&r.ID, &r.Name, &r.RoomType, &r.CreatedAt, &r.UpdatedAt, &r.DeletedAt,
        &r.GroupId, &r.BlockId, &r.ProjectId, &r.RoomLength, &r.RoomWidth, &r.CeilingHeight,
        &r.CeilingColorHex, &r.WallColorHex, &r.FloorColorHex, &r.CeilingColorRgb, &r.WallColorRgb,
        &r.FloorColorRgb)
    if err = rows.Err(); err != nil {
        panic(err)
    }

    fmt.Printf("Found: %v", r)
    rooms = append(rooms, r)
}

Yet, the resulting payload is:

{3 Loki #1 3 2018-09-25 08:42:38 +0000 UTC 2018-09-25 14:52:39 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0 0 0 0 0 0      }

I'm specifically after the length/width, which (above) are 0. Yet in the db:

mysql> select * from room where id = 3 \G
*************************** 1. row ***************************
               id: 3
             name: Loki #1
        room_type: 3
       created_at: 2018-09-25 08:42:38
       updated_at: 2018-09-25 14:52:39
       deleted_at: NULL
         group_id: 0
         block_id: 1
       project_id: 0
      room_length: 10
       room_width: 7
   ceiling_height: 4
ceiling_color_hex: #c0c0c0
   wall_color_hex: #a9a9a9
  floor_color_hex: #708090
ceiling_color_rgb: 192,192,192
   wall_color_rgb: 169,169,169
  floor_color_rgb: 112,128,144
1 row in set (0.00 sec)

I thought it may have something to do with differing types, but after changing them in the db, and then in code, no change. Can anyone explain why .Scan isn't picking up certain values?

Thanks!

  • 写回答

1条回答 默认 最新

  • dtj2ww9500 2018-09-25 16:00
    关注

    First of all, check the error which come from rows.Scan(). You only check the error from rows.Err() which is a different kind of error, and not related to the scan.

    err := rows.Scan(&r.ID, &r.Name, &r.RoomType, &r.CreatedAt, &r.UpdatedAt, &r.DeletedAt,
        &r.GroupId, &r.BlockId, &r.ProjectId, &r.RoomLength, &r.RoomWidth, &r.CeilingHeight,
        &r.CeilingColorHex, &r.WallColorHex, &r.FloorColorHex, &r.CeilingColorRgb, &r.WallColorRgb,
        &r.FloorColorRgb)
    if err != nil {
        panic(err) // Error related to the scan
    }
    if err = rows.Err(); err != nil {
        panic(err) // Error related to the iteration of rows
    }
    

    When the value from deleted_at is return as NULL, Scan will return an error such as unsupported Scan, storing driver.Value type <nil> into type *time.Time and the rest of your struct will get zero values.

    It means that your room struct has to change to use a pointer to time.

    CreatedAt       *time.Time
    UpdatedAt       *time.Time
    DeletedAt       *time.Time
    

    However, you may need to add into you sql.Open() the parameter parseTime sql.Open("mysql", "user:password@/dbname?parseTime=true) to parse the time correctly for mysql.

    You should then received a valid *time.Time when it is set, or nil when it is NULL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码