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

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

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还是我做错了什么?有人可以帮助我吗?

func GetQueues(uniqueId string) ([]*objects.Queue, error) {
    db, err := GetDBConnection()
    defer db.Close()

    rows, err := db.Query("SELECT qid, title, text, queue_length, user_id, date_created, last_updated FROM queue WHERE unique_id = ?", uniqueId)
    if err != nil {
        panic(err.Error())
    }
    defer rows.Close()

    var qId             string
    var title           string
    var text            string
    var userId          string
    var dateCreated     string
    var lastUpdated     string
    var queueLength     int64
    var queue           *objects.Queue
    var queues          []*objects.Queue

    for rows.Next() {
        err           = rows.Scan(&qId, &title, &text, &queueLength, &userId, &dateCreated, &lastUpdated)
        queue         = &objects.Queue{ QId: qId, Title: title, Text: text, UniqueId: uniqueId, UserId: userId, DateCreated: dateCreated, LastUpdated: lastUpdated, QueueLength: queueLength }
        queues        = append(queues, queue)
    }
    err = rows.Err()

    if err != nil {
        panic(err.Error())
        return nil, err
    }
    return queues, err
}
英文:

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?

func GetQueues(uniqueId string) ([]*objects.Queue, error) {
	db, err := GetDBConnection()
	defer db.Close()

	rows, err := db.Query("SELECT qid, title, text, queue_length, user_id, date_created, last_updated FROM queue WHERE unique_id = ?", uniqueId)
	if err != nil {
		panic(err.Error())
	}
	defer rows.Close()

	var qId             string
	var title           string
	var text            string
	var userId          string
	var dateCreated     string
	var lastUpdated     string
	var queueLength     int64
	var queue           *objects.Queue
	var queues          []*objects.Queue

	for rows.Next() {
		err           = rows.Scan(&qId, &title, &text, &queueLength, &userId, &dateCreated, &lastUpdated)
		queue         = &objects.Queue{ QId: qId, Title: title, Text: text, UniqueId: uniqueId, UserId: userId, DateCreated: dateCreated, LastUpdated: lastUpdated, QueueLength: queueLength }
		queues        = append(queues, queue)
	}
	err = rows.Err()

	if err != nil {
		panic(err.Error())
		return nil, err
	}
	return queues, err
}

答案1

得分: 4

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

var queueLength sql.NullInt64

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

英文:

queueLength should be a NullInt64 instead of an int64.

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:

确定