sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

MapScan got []uint8 for BIGINT and VARCHAR types

Open greensea opened this issue 3 years ago • 2 comments

I am selecting rows from MySQL, and using MapScan() to scan data. The row data is BIGINT and VARCHAR, so I am expecting to get int64 and string (or []byte) data. But I got []uint8 for those data. Here is the code:

package main

import (
	"fmt"
	"time"

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

var DSN string = "user:pass@tcp(192.168.1.88:3306)/db?charset=utf8mb4"

func main() {
	db, _ := sqlx.Open("mysql", DSN)
	db.Exec(`DOP TABLE t`)

	db.Exec(`CREATE TABLE t (
		id INT,
		name varchar(32)
	)`)

	db.Exec("INSERT INTO t (id, name) VALUES(?, ?)", time.Now().Unix(), "SomeName")

	row := db.QueryRowx("SELECT * FROM t")
	result := make(map[string]interface{})
	row.MapScan(result)

	fmt.Println(result)
}

The output is:

map[t.id:[49 54 52 52 57 57 57 50 49 54] t.name:[83 111 109 101 78 97 109 101]]
Data Type is: []uint8
Data Type is: []uint8

The name column is []uint8 data, ok that's acceptable, I can convert it to string manually. But the data of the id column is wired, it is timestamp "1644999985" represented in ASCII code. I think this is not the correct behavior.

greensea avatar Feb 16 '22 08:02 greensea

I think the issue is not specific to sqlx but its related to the mysql driver. See https://github.com/go-sql-driver/mysql/issues/407

ipsusila avatar Mar 19 '22 23:03 ipsusila

try add where 1 = 1. maybe can slove this problem.

s6816112 avatar Aug 22 '23 09:08 s6816112