gpt4 book ai didi

postgresql - 将数组插入 Postgresql 数据库

转载 作者:IT老高 更新时间:2023-10-28 13:05:38 24 4
gpt4 key购买 nike

我希望能够将一个 bigints 数组写入我在 Go 中用于历史记录的表中。不幸的是,我不能,当我执行错误 sql: convert Exec argument #1's type: unsupported type []int64, a slice 时抛出。这是我正在做的,为简洁起见编辑:

type Card struct {
cid int64
}

type Transaction struct {
tid, cardid int64
productids []int64
salepoint int
cardkey string
}

func logPurchase(card *Card, t *Transaction) {
_, err := db.Exec("INSERT INTO history VALUES ($1, $2, $3, $4)", rand.Int63(), t.productids, card.cid, t.salepoint);
}

这是我希望插入的表的结构:tid bigint 主键,productids bigint[] not null,cardid bigint not null,salepoint int

最佳答案

Arrays自 2016 年 8 月 6 日起在 github.com/lib/pq 中得到支持。 OP的声明可以写成:

_, err := db.Exec(
"INSERT INTO history VALUES ($1, $2, $3, $4)",
rand.Int63(),
pq.Array(t.productids), // <-------
card.cid,
t.salepoint,
)

关于postgresql - 将数组插入 Postgresql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26707553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com