Skip to content
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

It doesn't work for lastest golang version #349

Closed
zhonglushu opened this issue Aug 29, 2017 · 10 comments
Closed

It doesn't work for lastest golang version #349

zhonglushu opened this issue Aug 29, 2017 · 10 comments

Comments

@zhonglushu
Copy link

zhonglushu commented Aug 29, 2017

I just get some meaningless Numbers.

@jhngrant
Copy link

@zhonglushu are you able to say what database and driver are you using?

@zhonglushu
Copy link
Author

Mysql,table schema like this:
unknow2

@zhonglushu
Copy link
Author

@jhngrant
Copy link

@zhonglushu to try and replicate this issue, are you also able to share a snippet of Go code? - both the code you are code using to call sqlx and the struct.

In case you haven't already tried, try a forced rebuilding of packages in your project using:

go build -a

@zhonglushu
Copy link
Author

zhonglushu commented Aug 30, 2017

The sqlx code is:
` db, err := sqlx.Connect("mysql", "root:******@tcp(localhost:3306)/test?charset=utf8")
if err != nil {
panic(err.Error())
}

rows, err := db.Queryx("SELECT * FROM population")
	if err != nil {
		panic(err.Error())
	}
	m := map[string]interface{}{}
	for rows.Next() {
		err = rows.MapScan(m)
		if err != nil {
			panic(err.Error())
		}
		fmt.Println(m)
		fmt.Println("-------------")
	}`

the running result is:
unknow3

@zhonglushu
Copy link
Author

I found scan method can not convert columns read from the database into *interface{}

@jhngrant
Copy link

@zhonglushu I'm not seeing the same issue. Out of interesting, what does the output look like when you range over the map instead? :

	rows, err := db.Queryx("SELECT * FROM population")
	if err != nil {
		panic(err.Error())
	}
	m := map[string]interface{}{}
	for rows.Next() {
		err = rows.MapScan(m)
		if err != nil {
			panic(err.Error())
		}
		// fmt.Println(m)
		for i := range m {
			fmt.Printf("%v\n", m[i])
		}
                fmt.Println("-------------")
	}

@zhonglushu
Copy link
Author

zhonglushu commented Aug 30, 2017

It doesn't work,I found scan method can not convert columns read from the database into *interface{}.
For example, the code below can work, but the result is unexpectedly:

    var name interface{}
    var code int
    var year int
    var total float64
    err = rows.Scan(&name, &code, &year, &total)
    if err != nil {
        fmt.Println("Get values from database error")
        fmt.Println(err.Error())
    }
    popuMap := make(map[string]interface{})
    popuMap["Region_name"] = name
    popuMap["Region_code"] = code
    popuMap["Year"] = year
    popuMap["Total_population"] = total

After I modify the code "var name interface{}" like this:

    var name string
    var code int
    var year int
    var total float64
    err = rows.Scan(&name, &code, &year, &total)
    if err != nil {
        fmt.Println("Get values from database error")
        fmt.Println(err.Error())
    }
    popuMap := make(map[string]interface{})
    popuMap["Region_name"] = name
    popuMap["Region_code"] = code
    popuMap["Year"] = year
    popuMap["Total_population"] = total

I found the code runs properly, why?

@jhngrant
Copy link

jhngrant commented Aug 30, 2017

@zhonglushu This behavior seems to be specific to the 'github.com/go-sql-driver/mysql' driver.
See: go-sql-driver/mysql#366

@zhonglushu
Copy link
Author

@jhngrant Thank you very much!

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

2 participants