Home Articles

如何将.Scan()MySQL TIMESTAMP值转换为time.Time变量?

Asked
Viewed 383 times
5

我有这个Go代码:

package main

import (
    "fmt"
    "database/sql"
    _"github.com/go-sql-driver/mysql"
    "time"
)

type User struct {
    id                  uint32
    name                string
    email               string
    rating              uint8
    subscription        uint8
    date_registered     time.Time
    online              string
}

// main entry point
func main() {
    // setup db connection
    db, err := sql.Open("mysql", "user:@tcp(127.0.0.1:3306)/c9?parseTime=true")
    if err != nil {
        fmt.Println(err)
    }
    defer db.Close()

    // query
    rows, err := db.Query("SELECT * FROM users WHERE id = ?", 1)
    if err != nil {
        fmt.Println(err)
    }
    defer rows.Close()

    usr := User{}
    for rows.Next() {
        err := rows.Scan(&usr.id, &usr.name, &usr.email, &usr.rating, &usr.subscription, &usr.date_registered, &usr.online)
        if err != nil {
            fmt.Println(err)
        }
    }
    fmt.Println(usr)
    err = rows.Err()
    if err != nil {
        fmt.Println(err)
    }
}

这是我从MySQL控制台获得的:

mysql> describe users;
+-----------------+---------------------+------+-----+-------------------+----------------+
| Field           | Type                | Null | Key | Default           | Extra          |
+-----------------+---------------------+------+-----+-------------------+----------------+
| id              | int(10) unsigned    | NO   | PRI | NULL              | auto_increment |
| name            | varchar(50)         | NO   |     | NULL              |                |
| email           | varchar(50)         | NO   |     | NULL              |                |
| rating          | tinyint(3) unsigned | YES  |     | NULL              |                |
| subscription    | tinyint(3) unsigned | NO   |     | 0                 |                |
| date_registered | timestamp           | NO   |     | CURRENT_TIMESTAMP |                |
| online          | char(1)             | NO   |     | N                 |                |
+-----------------+---------------------+------+-----+-------------------+----------------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM users;
+----+------------+-----------------------+--------+--------------+---------------------+--------+
| id | name       | email                 | rating | subscription | date_registered     | online |
+----+------------+-----------------------+--------+--------------+---------------------+--------+
|  1 | alakhazamm | abcdefghhhh@gmail.com |   NULL |            0 | 2014-10-28 15:37:44 | N      |
+----+------------+-----------------------+--------+--------------+---------------------+--------+
1 row in set (0.00 sec)

.Scan() 之后, fmt.Println(usr) 打印

{1 alakhazamm abcdefghhhh@gmail.com 0 0 {0 0 <nil>} }

结构的最后两个字段是错误的,但我不知道为什么 . 我尝试在结构定义中使用 date_registered string ,但是在 .Scan() 之后我得到一个空字符串 . 我've also read in the driver'的文档 ?parseTime=true 将MySQL DATE和DATETIME值解析为time.Time,但是他们当前没有使用.1837341_m .

我错过了一些重要的内容,还是图书馆的错误/缺失功能?

2 Answers

  • 4

    我找到了错误的原因 .

    由于 rating 在数据库中为NULL,因此扫描程序发出错误

    sql:列索引3上的扫描错误:将字符串“nil”转换为uint8:strconv.ParseUint:解析“nil”:语法无效

    我已经更新了数据库行,现在 usr.date_registeredusr.online 保持正确的值 .

    我想我必须使MySQL字段为NOT NULL并且只使用-1来表示未初始化的值 .

  • 3

    我知道这是一个老问题但是我在Open公告中缺少这个参数:

    parseTime=true
    

    See here

Related