go - sql: scan row(s) with unknown number of columns (select * from ...) -


I have a table with many columns t , and my SQL is like this: < Html>

Code> Select * to T Now I want to scan only one or two columns with a wide returned row set. However, sql.Scan accepts logic as the dest ... interface {} . Does this mean that I have to scan everything and only need to use columns?

I know that I can convert the SQL from select * to my_favorite_rows However, in this case, I want to change the SQL Not the way.

Thank you, ElGs

You can use rows. Columns, such as

  package main imports ("database / sql" "fmt" "github.com/lib/pq") type vehicle structure {ID int name string wheels int} // vehicle call one The vehicle gives a reference to the vehicle's vehicle column (column string, VHH * vehicle) interface {} {switch column {case "id": return and vh. ID case "name": Return & amp; Vh.Name Case "Wheels": Back & amp; Vh.Wheels default: panic ("unknown column" + colname)}} func panicOnErr {error error} {if mistake! = Zero (Panic Error ())}} func main () {conn, err: = pq.ParseURL (`Postgreas: // Docker: Docker @ 172.17.0.2: 5432 / PGSALTS? Schema = Public ') PanicOnier (mistake) Var db * sql .DB db, err = sql.Open ("postg res", conn) panicOnErr (error) var rows * sql.Rows rows, err = db.query ("select * from vehicle") panicOnErr (err) // Get the column name from the query call columns [column], Err = rows.Columns () panicOnErr (err) colNum: = len (columns) all: = [] Vehicles for rows {}. Next () {VH: = Vehicle {} // reference with reference to curl VehicleCol cols: = make ([] interface {}, colNum) i: = 0; I & lt; ColNum; I ++ {cols [i] = vehiclecol (column [i], & vh)} err = rows.Scan (cols ...) panicOnErr (err) all = append (all, vh)} fmt.printf (" # V \ n ", all)}    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -