使用ziutek/mymysql在Go中选择NULL时间戳

huangapple go评论90阅读模式
英文:

Selecting NULL Timestamps In Go With ziutek/mymysql

问题

我有以下的Google Go和MySql代码,其中问题中的date_update字段经常返回NULL值。

var date_update time.Time
query := `SELECT date_update FROM users WHERE user_id=?`
err := conn.QueryRow(query, user_id).Scan(&date_update)

当我运行这个查询并且碰巧接收到字段的一个空值时,我会收到错误信息unsupported driver -> Scan pair: <nil> -> *time.Time

我处理其他数据类型(例如INT)的方式是使用以下语句:

SELECT IFNULL(integer_field, 0) FROM table

但是对于时间戳或日期时间似乎不起作用。有什么想法吗?

英文:

I have the following Google Go and MySql, where the date_update field in question often comes back as a NULL value.

var date_update time.Time
query := `SELECT date_update FROM users WHERE user_id=?`
err := conn.QueryRow(query, user_id).Scan(&amp;date_update)

When I run this query and happen to receive one of the null values for the field, I receive the error unsupported driver -&gt; Scan pair: &lt;nil&gt; -&gt; *time.Time

The way I handle this with other data types (for example INT) is with

SELECT IFNULL(integer_field, 0) FROM table

But it doesn't seem to work with timestamps or datetime. Any ideas?

答案1

得分: 5

这是一个讨论与你的问题几乎完全相同的问题的Google Groups讨论

这段内容摘自go-sql-drivers/mysql 仓库的README。

time.Time支持

MySQL DATE和DATETIME值的默认内部输出类型是[]byte,这允许你将值扫描到[]byte、string或sql.RawBytes变量中。

然而,许多人希望将MySQL DATE和DATETIME值扫描到time.Time变量中,在Go中,这与MySQL中的DATE和DATETIME是逻辑上相反的。你可以通过将内部输出类型从[]byte更改为time.Time,并使用DSN参数parseTime=true来实现。你可以使用loc DSN参数设置默认的time.Time位置。

注意:从Go 1.1开始,这将使time.Time成为你可以将DATE和DATETIME值扫描到的唯一变量类型。这会破坏sql.RawBytes的支持,例如。

或者,你可以使用NullTime类型作为扫描目标,它可以与time.Time和string / []byte一起使用。

你需要在DSN中添加parseTime=true,以便自动将其解析为time.Time,或者使用NullTime


使用mymysql

你可以尝试选择具有特定id的行,然后使用以下函数之一:

英文:

This is a google groups discussion that discusses a problem almost identical to yours.

This is the section is taken from the README of the go-sql-drivers/mysql repository.

> time.Time support
>
>The default internal output type of MySQL DATE and DATETIME values is []byte which allows you to scan the value into a []byte, string or sql.RawBytes variable in your programm.
>
>However, many want to scan MySQL DATE and DATETIME values into time.Time variables, which is the logical opposite in Go to DATE and DATETIME in MySQL. You can do that by changing the internal output type from []byte to time.Time with the DSN parameter parseTime=true. You can set the default time.Time location with the loc DSN parameter.
>
>Caution: As of Go 1.1, this makes time.Time the only variable type you can scan DATE and DATETIME values into. This breaks for example sql.RawBytes support.
>
>Alternatively you can use the NullTime type as the scan destination, which works with both time.Time and string / []byte.

You either have to add parseTime=true to your DSN in order for it to parse into a time.Time automatically or use NullTime.


Using mymysql

You could try selecting the row with a specific id & then use one of the following functions:

huangapple
  • 本文由 发表于 2014年8月19日 14:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/25377347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定