How to insert multi-dimensional arrays using pgx/v5 #2263
-
Hello! In my code I have two-dimensional slice of type [][]int32 which i cannot insert into postgres table. The table is like this: Doing something like: and an error looks like It is working if I use lib/pq: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Works for me. package main
import (
"context"
"log"
"os"
"github.com/jackc/pgx/v5"
)
func main() {
ctx := context.Background()
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer conn.Close(ctx)
_, err = conn.Exec(ctx, "create temporary table t (id bigint primary key, ary int[][]);")
if err != nil {
log.Fatal(err)
}
_, err = conn.Exec(ctx, "insert into t values (1, $1);", [][]int{{1, 2}, {3, 4}})
if err != nil {
log.Fatal(err)
}
var ary [][]int
err = conn.QueryRow(ctx, "select ary from t where id = 1;").Scan(&ary)
if err != nil {
log.Fatal(err)
}
log.Printf("ary: %v", ary)
}
|
Beta Was this translation helpful? Give feedback.
Works for me.