Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a row from a table to a map, Without knowing columns

Tags:

go

In my golang app i need to do SQL query to MySQL to get single row and put result in a map[string]string keys are column names.

But i don't know what will be columns. Query is like

SELECT * FROM mytable

I use "database/sql" .

I found only Scan function

db.QueryRow("SELECT * FROM mytable").Scan(&var1, &var2,...)

but this doesn't work for my condition. I don't know how many variables will be there. And also I need column names.

Is it possible to do with database/sql ?

Update. I found how to solve part of this problem. How to get column names from a result set.

rows, err := db.Query(sqlcommand)

cols, err := rows.Columns()

So, i can use to make keys for a map. But i still don't know how to get values. Because, values can have different type.

data = make(map[string]string)

if rows.Next() {
    columns := make([]interface{}, len(cols))
    columnPointers := make([]interface{}, len(cols))
    for i, _ := range columns {
        columnPointers[i] = &columns[i]
    }

    err = rows.Scan(columnPointers...)

    for i, colName := range cols {
        // value is in columns[i] of interface type.
        // How to extract it from here? 
        // ....
        data[colName] = val
    }
}

P.S. This question is not duplicate of "Get table column names in mysql? ". I wanted to get columns of returned data set, not just a table.

like image 685
Roman Gelembjuk Avatar asked Aug 07 '18 16:08

Roman Gelembjuk


People also ask

How do you read rows vs columns?

KEY DIFFERENCESA row is a series of data put out horizontally in a table or spreadsheet while a column is a vertical series of cells in a chart, table, or spreadsheet. Rows go across left to right. On the other hand, Columns are arranged from up to down.

How do you retrieve all the columns from a table?

You can use an asterisk character, *, to retrieve all the columns. In queries where all the data is found in one table, the FROM clause is where we specify the name of the table from which to retrieve rows. In other articles we will use it to retrieve rows from multiple tables.

How can you see all data from the table?

The first command you will need to use is the SELECT FROM MySQL statement that has the following syntax: SELECT * FROM table_name; This is a basic MySQL query which will tell the script to select all the records from the table_name table.


1 Answers

You seem to be on the right track. According to the definition of Rows.Scan, you can supply values of the desired destination type, which would be string here. So changing the type of columns to []string should work:

var db sql.DB
var sqlcommand string

rows, _ := db.Query(sqlcommand)
cols, _ := rows.Columns()

data := make(map[string]string)

if rows.Next() {
    columns := make([]string, len(cols))
    columnPointers := make([]interface{}, len(cols))
    for i, _ := range columns {
        columnPointers[i] = &columns[i]
    }

    rows.Scan(columnPointers...)

    for i, colName := range cols {
        data[colName] = columns[i]
    }
}
like image 108
robx Avatar answered Oct 22 '22 10:10

robx