jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.02k stars 1.07k forks source link

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

Open greensea opened 2 years ago

greensea commented 2 years ago

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 commented 2 years ago

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

s6816112 commented 1 year ago

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