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

rows.MapScan 方法是不是有BUG? #191

Closed
panjunjie opened this issue Nov 24, 2015 · 6 comments
Closed

rows.MapScan 方法是不是有BUG? #191

panjunjie opened this issue Nov 24, 2015 · 6 comments

Comments

@panjunjie
Copy link

我从 PostgreSQL 数据库,读取几条数据,并使用 MapScan 填充 []map[string]interface{}{},character varying 字段输出的结果是不正确的,代码如下:

func ArticleJson(w http.ResponseWriter, req *http.Request) {
ret := make([]map[string]interface{}, 0)
rows, err := db.Queryx("SELECT id,title FROM article")
if err == nil {
    for rows.Next() {
        results := make(map[string]interface{})
        rows.MapScan(results)
        ret = append(ret, results)
    }
}
r.JSON(w, http.StatusOK, ret)
}

输出的结果如下:
[{"id":1,"title":"aXQgaXMgbXkgYmxvZw=="},{"id":2,"title":"eW91IGFyZSBvaz8="}]

但是实际上结果应该是:
[{"id":1,"title":"this is my article!"},{"id":2,"title":"are you ok?"}]

我这么使用,有什么问题吗?还是 sqlx 有bug?

@jmoiron
Copy link
Owner

jmoiron commented Dec 11, 2015

Sorry I can't reply in Chinese:

It seems like your values are base64 encoded. This is likely done by the driver, or perhaps by some kind of middlewhere somewhere, if this is coming from the web.

>>> "aXQgaXMgbXkgYmxvZw==".decode("base64")
'it is my blog'
>>> "eW91IGFyZSBvaz8=".decode("base64")
'you are ok?'

Or in Go:

b, _ := base64.StdEncoding.DecodeString("aXQgaXMgbXkgYmxvZw==")
fmt.Println(string(b))

This is not sqlx or database/sql doing this automatically though; either the database at the source has this encoding or there is something else making it return values in that encoding.

@jmoiron jmoiron closed this as completed Dec 11, 2015
@panjunjie
Copy link
Author

好的,我已经解决了这个问题。Thank you. The problem has been resolved.

@zbird
Copy link

zbird commented Jul 25, 2017

@panjunjie @jmoiron 俊杰,怎么解决的?我最近也碰到这个问题,numberic(14,3)就变成了[]byte类型了。。
How to avoid this issue? i get []byte when the column type is number(14,3)

@LuckyChen666
Copy link

好的,我已经解决了这个问题。Thank you. The problem has been resolved.

@panjunjie 你是怎么解决这个问题的呀

@long2ice
Copy link

long2ice commented Jul 1, 2022

解决了问题又不说解决方法,这种人真是自私,只想别人帮助自己,自己解决了就藏着掖着

@long2ice
Copy link

long2ice commented Jul 1, 2022

解决了问题又不说解决方法,这种人真是自私,只想别人帮助自己,自己解决了就藏着掖着

解决方法在这里:#225

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

5 participants