登录
首页 >  Golang >  Go问答

Golang Mysql 查询在数据存在时返回零的问题

来源:stackoverflow

时间:2024-03-02 08:42:26 226浏览 收藏

一分耕耘,一分收获!既然都打开这篇《Golang Mysql 查询在数据存在时返回零的问题》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

有一点令人头疼的时刻。我有以下结构:

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
}

在 rest api 中读取它:

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)
}

然而,生成的有效负载是:

{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      }

我特别关注长度/宽度,(上面)为 0。但在数据库中:

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)

我认为这可能与不同的类型有关,但是在数据库中更改它们,然后在代码中更改它们后,没有任何更改。谁能解释为什么 .scan 没有选取某些值?

谢谢!


解决方案


首先,检查来自rows.Scan()的错误。您只检查来自rows.Err()的错误,这是一种不同类型的错误,与扫描无关。

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
}

deleted_at 的值作为 null 返回时,scan 将返回错误,例如 unsupported scan,将 driver.value 类型 存储到类型 *time.time 中,并且结构的其余部分将获得零值 em>.

这意味着您的房间结构必须更改为使用指向时间的指针。

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

但是,您可能需要在 sql.open() 中添加 parameter parseTime sql.open("mysql", "user:password@/dbname?parsetime=true) 才能正确解析 mysql 的时间。 p>

然后,您应该收到一个有效的 *time.time(当它被设置时),或 nil(当它为 null 时)。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Golang Mysql 查询在数据存在时返回零的问题》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>