Skip to content

mysql scan rows to map[string]interface,type DECIMAL is string #4466 #5783

@lizhenyu0128

Description

@lizhenyu0128

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

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions