Inserting an array into a Postgresql database

前端 未结 2 1961
独厮守ぢ
独厮守ぢ 2021-02-12 15:29

I want to be able to write an array of bigints into a table that I am using for history in Go. Unfortunately, I can\'t and when I do the error sql: converting Exec argumen

相关标签:
2条回答
  • 2021-02-12 15:50

    Arrays are supported in github.com/lib/pq since 2016 Aug 6th. OP's statement could be written as:

    _, err := db.Exec(
        "INSERT INTO history VALUES ($1, $2, $3, $4)", 
        rand.Int63(), 
        pq.Array(t.productids),   // <------- 
        card.cid, 
        t.salepoint,
    )
    
    0 讨论(0)
  • 2021-02-12 15:59

    Implement database/sql/driver.Valuer with a custom type:

    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.
    }
    
    0 讨论(0)
提交回复
热议问题