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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作