如何在Go中将SQL数组扫描到[]int64中?

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

How to scan SQL array to []int64 in Go?

问题

我正在从Postgres数据库中扫描一个int数组,它以[]uint8的形式返回。我需要将它们转换为[]int64,或者如何从数据库中以[]int64的形式返回它们?在我的查询中,我使用Postgres的Array函数进行选择:Array(col1),其中col1是serial类型。

我得到的错误是:

不支持的扫描,将driver.Value类型的`[]uint8`存储到类型`[]int64`中。
英文:

I am scanning an array of int from Postgres DB and it is returning as []uint8. I need them in []int64, how can I convert them into []int64 or how can I return them from the DB as []int64? In my query I am selecting using the Array function in Postgres: Array(col1) where col1 is serial.

The error I am getting is:

unsupported Scan, storing driver.Value type []uint8 into type []int64

答案1

得分: 8

如果你正在使用github.com/lib/pq,只需使用Int64Array

col1arr := []int64{}
arr := pq.Int64Array{}
err := rows.Scan(&arr)
// ...
col1arr = []int64(arr)
英文:

If you're using github.com/lib/pq, just use Int64Array.

col1arr := []int64{}
arr := pq.Int64Array{}
err := rows.Scan(&arr)
// ...
col1arr = []int64(arr)

huangapple
  • 本文由 发表于 2016年12月1日 17:25:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/40906802.html
匿名

发表评论

匿名网友

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

确定