I just started to work with golang/pgx and I need to scan all columns simply to send them to stdout. Obviously, there is no schema known at compile time nor the structure to scan. Any chance I can do it with pgx or any other golang/pgsql driver?
Asked
Active
Viewed 1,260 times
0
-
@mkopriva, i meant that in case the schema was known there wouldn't be a problem to scan data into the sctuct – Dzmitry Tsitavets Feb 19 '20 at 21:11
-
I don't know about pgx but `database/sql.Rows` has the [`ColumnTypes`](https://golang.org/pkg/database/sql/#Rows.ColumnTypes) method which you can use, together with the `reflect` package, to initialize the values into which you would scan the result of a query. – mkopriva Feb 19 '20 at 21:16
1 Answers
1
Late response: Just started with pgx
Once you have called rows.Next() and checked if it is true you can do something like below
for rows.Next() {
columnValues, _ := rows.Values()
for i, v := range columnValues {
fmt.Printf("Type of value at %v=%T, value=%v | ", i, v, v)
}
}
This will get the column values from your row into columnValues which you range over and print.
In my case with just one row it prints
Type of value at 0=int32, value=16 | Type of value at 1=string, value=fsdfsfsad | Type of value at 2=string, value=something | Type of value at 3=time.Time, value=2020-12-27 05:12:55.995975 +0000 UTC |

ykesh
- 1,199
- 2
- 21
- 33