duanke1984 2017-03-30 06:18
浏览 108

在golang中扫描到struct时得到err时间戳

what did I do: 1.exec sql query and get err timestamp data when scan to struct after db ddl. sql: SELECT state,round,remark,ctime FROM archive_track WHERE aid=? ORDER BY id DESC struct:

type Archive struct {
    Timestamp time.Time `json:"timestamp"`
    State int `json:"state"`
    Round int `json:"round"`
    Remark string `json:"remark,omitempty"`
}

go code:

a:=&Archive{}
 rows.Scan(&a.State, &a.Round, &a.Remark, &a.Timestamp)

mysql table info:

CREATE TABLE `archive_track` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT ,
  `aid` int(11) NOT NULL DEFAULT '0' ,
  `state` tinyint(11) DEFAULT NULL,
  `round` tinyint(4) NOT NULL DEFAULT '0' ,
  `remark` text COMMENT '其他信息',
  `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  `mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  `attribute` mediumint(8) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `ix_aid` (`aid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

table data:

| id | aid |state | round |remark | ctime | mtime | attribute |

|:---|---|---|---| ---| ---|--- | ---:|

| 1 | 1 | 1 | 1| "" | 2017-03-27 17:07:10 | 2017-03-27 17:07:10 | 0|

I can get correct data first:

&a{aid:1,state:1,round:1 ,ctime:1490605630} but get err data after db ttl(modify state(tinyint) to state(int) ):

&a{aid:0,state:0,round:0,ctime:-62135596800}.

db ttl:

alter table archive_track modify state int

I can't get correct data anymore until I restart the program. after I restart ,I can get correct data and err data don't appear anymore ,but why.It make me confuse.

Configuration

*Driver version (or git SHA):QD6LqgLz2JMxXqns8TaxtK9AuHs=

*Go version:go1.7.4

Server version: E.g. MySQL 5.6,

*Server OS:Linux version 3.16.0-4-amd64

  • 写回答

1条回答 默认 最新

  • download12749 2017-03-31 09:01
    关注

    If you're using gomysql as your database driver, then it seems to be that you don't have the parseTime DSN parameter enabled for the driver when you connect to your database.

    By default, gomysql does not scan the Go type time.Time into a struct, but it's implemented as a DSN parameter. Try this connection string:

    connectionString := fmt.Sprintf(`%s:%s@tcp(%s:%s)/%s?parseTime=true`,
            DBUsername,
            DBPassword,
            DBHost,
            DBHostPort,
            DBName)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序