Hi,
something like (not tested): 

package main

import (
        "database/sql"
        "fmt"
        "log"

        _ "github.com/go-sql-driver/mysql"
)

func main() {
//Connect to database and check for errors  
        db, err := sql.Open("mysql",
                "script:scriptPassword@tcp($HOSTNAME:3306)/Testing")
        if err != nil {
                log.Println(err)
        }

        var str string
        rows, err = db.Query("SELECT * FROM animals").Scan(&str)
        if err != nil && err != sql.ErrNoRows {
                log.Fatal(err)
        }

        cols, err := rows.Columns() // Remember to check err afterwards
        if err != nil {
                log.Fatal(err)
        }

        vals := make([]interface{}, len(cols))

        for rows.Next() {

        for i := range cols {
            vals[i] = &vals[i]

}

        err = rows.Scan(vals...)
        // Now you can check each element of vals for nil-ness,
        if err != nil {
                log.Fatal(err)
        }

        for i := range cols {

                    fmt.Println(vals[i]) 

                }
        }


Jamil Djadala


On Friday, September 15, 2017 at 2:59:59 AM UTC+3, Alexandre K wrote:
>
> Hello Everyone,
>
> I'm new to Golang and I am trying to figure out how to retrieve multiple 
> unknown columns and rows from a table.
>
> I have an example table called ANIMALS 
> <https://gist.github.com/akalaj/015aa79b5854b728af0baf884f50b827>.
>
> I am trying to follow the example at the bottom of this GUIDE 
> <http://go-database-sql.org/varcols.html>.
>
> Here is the code that I've sewn together:
>
> package main
>
> import (
>         "database/sql"
>         "fmt"
>         "log"
>
>         _ "github.com/go-sql-driver/mysql"
> )
>
> func main() {
> //Connect to database and check for errors  
>         db, err := sql.Open("mysql",
>                 "script:scriptPassword@tcp($HOSTNAME:3306)/Testing")
>         if err != nil {
>                 log.Println(err)
>         }
>
>         var str string
>         rows, err = db.Query("SELECT * FROM animals").Scan(&str)
>         if err != nil && err != sql.ErrNoRows {
>                 log.Fatal(err)
>         }
>
>         cols, err := rows.Columns() // Remember to check err afterwards
>         if err != nil {
>                 log.Fatal(err)
>         }
>
>         vals := make([]interface{}, len(cols))
>         for i, _ := range cols {
>         vals[i] = new(sql.RawBytes)
> }
>
>         for rows.Next() {
>         err = rows.Scan(vals...)
>         // Now you can check each element of vals for nil-ness,
>         if err != nil {
>                 log.Fatal(err)
>         }
>         // Here is where I get lost
>         // How should I access Vals and print the values it finds???
>         }
>
>
>
> I am trying to get Golang to print the values to the linux terminal using 
> something like the code below.
>
> fmt.Println(mysqlValues)
>
> How do I retrieve the MySQL values stored in the interface in m 
> <https://gist.github.com/akalaj/28684690c42da8e9aa0cea5bd7340b1c>y code 
> provided above????
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to