FAQ
Hey!

How to put SQL rows into an array of string arrays? (/convert SQL result to
string matrix)

Thanks and best regards
Frank

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Frank Blechschmidt at Mar 11, 2013 at 9:08 pm
    Found it.

    Working solution:
    var result [][]string
    rows := repo.dbHandler.Query(sQuery)
    cols := rows.Columns()
    pointers := make([]interface{}, len(cols))
    container := make([]string, len(cols))
    for i, _ := range pointers {
    pointers[i] = &container[i]
    }
    for rows.Next() {
    rows.Scan(pointers...)
    result = append(result, container)
    }





    On Monday, March 11, 2013 9:34:49 PM UTC+1, Frank Blechschmidt wrote:

    Hey!

    How to put SQL rows into an array of string arrays? (/convert SQL result
    to string matrix)

    Thanks and best regards
    Frank
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jksmithiii at May 10, 2013 at 9:54 pm
    Hey Frank!

    Slick way to cut the code when rawbytes won't work for the driver.

    This test however: PrintLn(result)
    after the loop doesn't yield expected results. It just repeats the result
    set number of rows of the last row that was read. If I just
    PrintLn(container) in the loop, all the rows print. Maybe it's just a bug
    in PrintLn for arrays of strings.
    On Monday, March 11, 2013 4:08:15 PM UTC-5, Frank Blechschmidt wrote:

    Found it.

    Working solution:
    var result [][]string
    rows := repo.dbHandler.Query(sQuery)
    cols := rows.Columns()
    pointers := make([]interface{}, len(cols))
    container := make([]string, len(cols))
    for i, _ := range pointers {
    pointers[i] = &container[i]
    }
    for rows.Next() {
    rows.Scan(pointers...)
    result = append(result, container)
    }





    On Monday, March 11, 2013 9:34:49 PM UTC+1, Frank Blechschmidt wrote:

    Hey!

    How to put SQL rows into an array of string arrays? (/convert SQL result
    to string matrix)

    Thanks and best regards
    Frank
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Julien Schmidt at May 11, 2013 at 2:34 am
    You are reusing the pointers so effectively you are overriding the content
    of the existing slices.
    Something like this should work: http://play.golang.org/p/_mdS9X3USU

    But I'd definitely recommend to use sql.RawBytes when ever possible.
    Moreover you run into problems if a nil value occurs. In this case using a
    bytes slice or sql.NullString instead of string might be the better option.
    On Friday, May 10, 2013 7:54:37 PM UTC+2, jksmi...@gmail.com wrote:

    Hey Frank!

    Slick way to cut the code when rawbytes won't work for the driver.

    This test however: PrintLn(result)
    after the loop doesn't yield expected results. It just repeats the result
    set number of rows of the last row that was read. If I just
    PrintLn(container) in the loop, all the rows print. Maybe it's just a bug
    in PrintLn for arrays of strings.
    On Monday, March 11, 2013 4:08:15 PM UTC-5, Frank Blechschmidt wrote:

    Found it.

    Working solution:
    var result [][]string
    rows := repo.dbHandler.Query(sQuery)
    cols := rows.Columns()
    pointers := make([]interface{}, len(cols))
    container := make([]string, len(cols))
    for i, _ := range pointers {
    pointers[i] = &container[i]
    }
    for rows.Next() {
    rows.Scan(pointers...)
    result = append(result, container)
    }





    On Monday, March 11, 2013 9:34:49 PM UTC+1, Frank Blechschmidt wrote:

    Hey!

    How to put SQL rows into an array of string arrays? (/convert SQL result
    to string matrix)

    Thanks and best regards
    Frank
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Aras Can Akın at Mar 7, 2016 at 7:45 pm
    I tried the []interface{} variant but it doesn't seem to be working. Can
    you help with this?

    cols, _ := rows.Columns()

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

    for i, _ := range pointers {
            pointers[i] = &container[i]
    }

    defer rows.Close()
    for rows.Next() {

         fmt.Println(container)

    }


    it prints
    [<nil> [102 99 54 48 50 48 48 53 45 100 55 52 97 45 52 49 51 52 45 55 55 100
    50 45 100 54 51 49 102 100 99 48 101 50 55 98] [114 111 98 101 114 116 64
    100 101 45 110 105 114 111 46 99 111 109] [82 111 98 101 114 116 32 68 101
    32 78 105 114 111] [114 100 110]]

    where each value should be string or time.Time
    On Monday, March 11, 2013 at 11:08:15 PM UTC+2, Frank Blechschmidt wrote:

    Found it.

    Working solution:
    var result [][]string
    rows := repo.dbHandler.Query(sQuery)
    cols := rows.Columns()
    pointers := make([]interface{}, len(cols))
    container := make([]string, len(cols))
    for i, _ := range pointers {
    pointers[i] = &container[i]
    }
    for rows.Next() {
    rows.Scan(pointers...)
    result = append(result, container)
    }





    On Monday, March 11, 2013 9:34:49 PM UTC+1, Frank Blechschmidt wrote:

    Hey!

    How to put SQL rows into an array of string arrays? (/convert SQL result
    to string matrix)

    Thanks and best regards
    Frank
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Djadala at Mar 8, 2016 at 4:36 am
    you miss scan:
    `err = rows.Scan(pointers...)`
    On Monday, March 7, 2016 at 9:46:16 PM UTC+2, Aras Can Akın wrote:

    I tried the []interface{} variant but it doesn't seem to be working. Can
    you help with this?

    cols, _ := rows.Columns()

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

    for i, _ := range pointers {
    pointers[i] = &container[i]
    }

    defer rows.Close()
    for rows.Next() {

    fmt.Println(container)

    }


    it prints
    [<nil> [102 99 54 48 50 48 48 53 45 100 55 52 97 45 52 49 51 52 45 55 55
    100 50 45 100 54 51 49 102 100 99 48 101 50 55 98] [114 111 98 101 114 116
    64 100 101 45 110 105 114 111 46 99 111 109] [82 111 98 101 114 116 32 68
    101 32 78 105 114 111] [114 100 110]]

    where each value should be string or time.Time
    On Monday, March 11, 2013 at 11:08:15 PM UTC+2, Frank Blechschmidt wrote:

    Found it.

    Working solution:
    var result [][]string
    rows := repo.dbHandler.Query(sQuery)
    cols := rows.Columns()
    pointers := make([]interface{}, len(cols))
    container := make([]string, len(cols))
    for i, _ := range pointers {
    pointers[i] = &container[i]
    }
    for rows.Next() {
    rows.Scan(pointers...)
    result = append(result, container)
    }





    On Monday, March 11, 2013 9:34:49 PM UTC+1, Frank Blechschmidt wrote:

    Hey!

    How to put SQL rows into an array of string arrays? (/convert SQL result
    to string matrix)

    Thanks and best regards
    Frank
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 11, '13 at 8:34p
activeMar 8, '16 at 4:36a
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase