-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Closed
Labels
type:with reproduction stepswith reproduction stepswith reproduction steps
Description
GORM Other Playground Link
go-gorm/playground#337
mysql: scan rows to map[string]interface,type DECIMAL is string and is wrong
If you know the table structure and define the structure, the data will be printed correctly. But if the table structure is unknown,then the data will be printed incorrectly.
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, err
}
numColumns := len(columns)
values := make([]interface{}, numColumns)
for i := range values {
values[i] = new(interface{})
}
var results []map[string]interface{}
for rows.Next() {
if err := rows.Scan(values...); err != nil {
return nil, err
}
dest := make(map[string]interface{}, numColumns)
for i, column := range columns {
fmt.Println(reflect.TypeOf(values[i]))
fmt.Println(values[i].(*interface{}))
fmt.Println(*values[i].(*interface{}))
dest[column] = values[i]
}
//fmt.Println("=====")
//fmt.Println(dest)
//print: 1234.5677 right
//print: NzMxLjI= wrong
results = append(results, dest) //ps: NzMxLjI=
}
if err := rows.Err(); err != nil {
return nil, err
}
return results, nil
gorm.io/gorm v1.24.0
Metadata
Metadata
Assignees
Labels
type:with reproduction stepswith reproduction stepswith reproduction steps