为什么 go-sql-driver 在处理 MySQL bigint 字段的 NULL 值时失败?

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

Why go-sql-driver fails to handle NULL in MySQL bigint field?

问题

我正在使用go-sql-driver连接到MySQL数据库。在我的一个表中,我使用了一个名为queue_length的BIGINT字段。默认情况下,该字段为NULL。当我尝试使用Golang中的go-sql-driver查询表中的所有字段时,结果集中包括queue_length在内的字段都没有显示出来。

在我的用例中,

表字段
[unique_id, qid, title, text, queue_length, user_id, date_created, last_updated]

当我执行以下代码时,我可以获取到queue_length之前的字段的值,但是结果集中没有[queue_length, user_id, date_created, last_updated]这些字段。

但是,如果我不选择queue_length,所有字段都会显示在结果集中。我在代码中将queue_length扫描到类型为int64的queueLength变量中。来自表的值为NULL。我尝试过类型转换,但没有成功。

这是go-sql-driver的一个bug还是我做错了什么?有人可以帮助我吗?

  1. func GetQueues(uniqueId string) ([]*objects.Queue, error) {
  2. db, err := GetDBConnection()
  3. defer db.Close()
  4. rows, err := db.Query("SELECT qid, title, text, queue_length, user_id, date_created, last_updated FROM queue WHERE unique_id = ?", uniqueId)
  5. if err != nil {
  6. panic(err.Error())
  7. }
  8. defer rows.Close()
  9. var qId string
  10. var title string
  11. var text string
  12. var userId string
  13. var dateCreated string
  14. var lastUpdated string
  15. var queueLength int64
  16. var queue *objects.Queue
  17. var queues []*objects.Queue
  18. for rows.Next() {
  19. err = rows.Scan(&qId, &title, &text, &queueLength, &userId, &dateCreated, &lastUpdated)
  20. queue = &objects.Queue{ QId: qId, Title: title, Text: text, UniqueId: uniqueId, UserId: userId, DateCreated: dateCreated, LastUpdated: lastUpdated, QueueLength: queueLength }
  21. queues = append(queues, queue)
  22. }
  23. err = rows.Err()
  24. if err != nil {
  25. panic(err.Error())
  26. return nil, err
  27. }
  28. return queues, err
  29. }
英文:

I am using go-sql-driver to connect to MySQL database. In one of my table I am using a field called queue_length as BIGINT. This field is NULL by default. When I try to query all the fields from the table using go-sql-driver in Golang, the fields after queue_length are not coming in the result set including queue_length.

In my use case,

Table fields
[unique_id, qid, title, text, queue_length, user_id, date_created, last_updated]

When I execute the following code I am getting values for fields before queue_length but [queue_length, user_id, date_created, last_updated] fields are not coming in result set.

But if I don't select queue_length, all fields are coming in result set. I am scanning queue_length in queueLength variable in code which is of type int64. The value coming from table is NULL. I tried type conversion also. Didn't work.

Is this a bug in go-sql-driver or am I doing anything wrong? Could anyone help me with this?

  1. func GetQueues(uniqueId string) ([]*objects.Queue, error) {
  2. db, err := GetDBConnection()
  3. defer db.Close()
  4. rows, err := db.Query("SELECT qid, title, text, queue_length, user_id, date_created, last_updated FROM queue WHERE unique_id = ?", uniqueId)
  5. if err != nil {
  6. panic(err.Error())
  7. }
  8. defer rows.Close()
  9. var qId string
  10. var title string
  11. var text string
  12. var userId string
  13. var dateCreated string
  14. var lastUpdated string
  15. var queueLength int64
  16. var queue *objects.Queue
  17. var queues []*objects.Queue
  18. for rows.Next() {
  19. err = rows.Scan(&qId, &title, &text, &queueLength, &userId, &dateCreated, &lastUpdated)
  20. queue = &objects.Queue{ QId: qId, Title: title, Text: text, UniqueId: uniqueId, UserId: userId, DateCreated: dateCreated, LastUpdated: lastUpdated, QueueLength: queueLength }
  21. queues = append(queues, queue)
  22. }
  23. err = rows.Err()
  24. if err != nil {
  25. panic(err.Error())
  26. return nil, err
  27. }
  28. return queues, err
  29. }

答案1

得分: 4

queueLength 应该是一个 NullInt64,而不是一个 int64

  1. var queueLength sql.NullInt64

在 Go 中,int 不能为 nil,所以 Scan 无法将 NULL 保存在 queueLength 中。

英文:

queueLength should be a NullInt64 instead of an int64.

  1. var queueLength sql.NullInt64

In Go ints can't be nil so there's no way for Scan to save NULL in queueLength.

答案2

得分: 0

我使用这个工具来处理空值和零值字段,它包含了许多数据类型。

GitHub的链接是https://github.com/guregu/null。

英文:

I use this tool for Null and Zero fields, it includes many data types.

The github url is https://github.com/guregu/null

huangapple
  • 本文由 发表于 2015年12月18日 15:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/34350074.html
匿名

发表评论

匿名网友

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

确定