Wednesday, 15 June 2011

mysql - Go SQL driver get interface{} column values -


I am trying to use the go sql driver to read from database tables and I would like the value to be [ ] Map [String] Interface {} . The name of the column is the key of the map and the value is of interface {} . I am adding all the columns to an array. I am using code sample for "RawBytes", for example to start with.

However, in the example, all column values ​​are converted to string as the following form,

rows for rows get receive. Next () {// Get Robots from Data = Rows Root. Scan (scan ARG ...) if mistake! = Zero (err.Error ()) // Due to terror in your app proper error handling // Now do something with data. // Here we print each column as a string. Var for var Value string, col: = range values ​​{// Here we can check that the value is zero (zero value) if col == zero {value = "NULL"} else {value = string (col) // ATTN: Here converted to string} fmt.Println (column [i], ":", value)} fmt.println ("---------------------- ------------ ")}

do it for interface {} There is a way to keep it in the form, so I can do the necessary [] map [string] interface {}

See the answer that is based on my answer. You can do something like this:

  var myMap = make (map [string] interface {}) Suspend the rows, err: = db.Query ("SELECT *) from myTable" (turn off) if mistake! = Zero  

This will work with generic and any table, number of columns etc.


No comments:

Post a Comment