我希望能够将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

最佳答案

使用自定义类型实现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.
}

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

10-16 07:03