Skip to content

MapScan got []uint8 for BIGINT and VARCHAR types #801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
greensea opened this issue Feb 16, 2022 · 2 comments
Open

MapScan got []uint8 for BIGINT and VARCHAR types #801

greensea opened this issue Feb 16, 2022 · 2 comments

Comments

@greensea
Copy link

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.

@ipsusila
Copy link

I think the issue is not specific to sqlx but its related to the mysql driver. See go-sql-driver/mysql#407

@s6816112
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants