dongrui6787 2019-04-04 10:41
浏览 83
已采纳

无法将mysql中的时间戳值提取到time.Time变量中

I cannot get mysql timestamp value in into a time.Time variable

I am trying to scan a row, All values get successfully scanned except the timestamp type in mysql, I am already using dsn parseTime=true which was one of the issues i searched into, but it does not help

        type Tags struct {
            ID             int
            CreatedAt      time.Time `json:"created_at"`
        }
        func foo5() {
            http.HandleFunc("/tags/", bar5)
            http.ListenAndServe(":8080", nil)
        }

        func bar5(w http.ResponseWriter, r *http.Request) {
            db, err := sql.Open("mysql", "root:*******@/catalogue_service?parseTime=true")
            if err != nil {
            fmt.Println("error opening")
            }
            v, _ := strconv.Atoi(path.Base(r.URL.Path))
            row := db.QueryRow("select * from tags where id = ?", v)
            var myRow Tags
            row.Scan(&myRow.ID, &myRow.CreatedAt)
            fmt.Println(myRow)
        }

int and string values are displayed properly but CreatedAt prints 0001-01-01 00:00:00 +0000 UTC instead of the actual value

Edit:

 +-----------------+--------------------------+------+-----+-------------------+-----------------------------------------------+
| Field           | Type                     | Null | Key | Default           | Extra                                         |
+-----------------+--------------------------+------+-----+-------------------+-----------------------------------------------+
| id              | int(11)                  | NO   | PRI | NULL              | auto_increment                                |
| created_at      | timestamp                | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED                             |
+-----------------+--------------------------+------+-----+-------------------+-----------------------------------------------+

| 84 | 2019-01-30 11:21:36 |

  • 写回答

2条回答 默认 最新

  • douzhao9608 2019-04-04 13:05
    关注

    Error has been resolved by myself, there were null values in the table which Scan could not handle hence i used sql.NULLString or mysql.NULLTime type in struct depending on the field

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同