小编典典

将数组插入Postgresql数据库

go

我希望能够将bigints数组写入要用于Go历史记录的表中。不幸的是,我不能,当我这样做时`sql: converting 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 primary key, productids bigint[] not null, cardid bigint not null, salepoint int


阅读 256

收藏
2020-07-02

共1个答案

小编典典

使用自定义类型实现database / sql / driver.Valuer:

type int64array []int64

func (a int64array) Value() (driver.Value, error) {
    // Format a in PostgreSQL's array input format {1,2,3} and return it as as string or []byte.
}
2020-07-02