英文:
How to share mysql connection using sqlx?
问题
我想分享一个可以在其他函数中使用的sqlx连接。以下是我想到的代码:
var dbmap = initDb()
func initDb() *sqlx.DB {
db, err := sqlx.Connect("mysql", "myuser:mypasswd@tcp(127.0.0.1:3306)/mydb")
if err != nil {
log.Fatalln(err)
}
// defer db.Close()
return db
}
然后在函数中,我想执行查询操作,例如:
var users []User
err := dbmap.Get(&users, "SELECT * FROM user")
然而,我遇到了恐慌错误:
scannable dest type slice with >1 columns (3) in result
2017/07/10 09:32:59 [Recovery] panic recovered:
GET /api/v1/users HTTP/1.1
Host: 127.0.0.1:8080
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cache-Control: max-age=0
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
scannable dest type slice with >1 columns (3) in result
/usr/lib/go-1.6/src/runtime/panic.go:443 (0x436179)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/lib/go-1.6/src/log/log.go:320 (0x46a551)
Panic: panic(s)
/home/me/go/src/myapp/main.go:84 (0x4098bd)
GetUsers: log.Panic(err)
/home/me/go/src/github.com/gin-gonic/gin/context.go:97 (0x47773a)
(*Context).Next: c.handlers[c.index](c)
/home/me/go/src/myapp/main.go:54 (0x40b170)
Cors.func1: c.Next()
/home/me/go/src/github.com/gin-gonic/gin/context.go:97 (0x47773a)
(*Context).Next: c.handlers[c.index](c)
/home/me/go/src/github.com/gin-gonic/gin/recovery.go:45 (0x489fd1)
RecoveryWithWriter.func1: c.Next()
/home/me/go/src/github.com/gin-gonic/gin/context.go:97 (0x47773a)
(*Context).Next: c.handlers[c.index](c)
/home/me/go/src/github.com/gin-gonic/gin/logger.go:63 (0x48926a)
LoggerWithWriter.func1: c.Next()
/home/me/go/src/github.com/gin-gonic/gin/context.go:97 (0x47773a)
(*Context).Next: c.handlers[c.index](c)
/home/me/go/src/github.com/gin-gonic/gin/gin.go:284 (0x47e592)
(*Engine).handleHTTPRequest: context.Next()
/home/me/go/src/github.com/gin-gonic/gin/gin.go:265 (0x47e1c7)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/lib/go-1.6/src/net/http/server.go:2081 (0x60372e)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/lib/go-1.6/src/net/http/server.go:1472 (0x5fffde)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/lib/go-1.6/src/runtime/asm_amd64.s:1998 (0x467171)
goexit: BYTE $0x90 // NOP
这里出了什么问题?我该如何修复它?
英文:
I'd like to share sqlx connection to be used in other functions. Here is what I came up with:
var dbmap = initDb()
func initDb() *sqlx.DB {
db, err := sqlx.Connect("mysql", "myuser:mypasswd@tcp(127.0.0.1:3306)/mydb")
if err != nil {
log.Fatalln(err)
}
// defer db.Close()
return db
}
And then in functions I'd like to make queries like:
var users []User
err := dbmap.Get(&users, "SELECT * FROM user")
However I get panic:
scannable dest type slice with >1 columns (3) in result
2017/07/10 09:32:59 [Recovery] panic recovered:
GET /api/v1/users HTTP/1.1
Host: 127.0.0.1:8080
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cache-Control: max-age=0
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
scannable dest type slice with >1 columns (3) in result
/usr/lib/go-1.6/src/runtime/panic.go:443 (0x436179)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/lib/go-1.6/src/log/log.go:320 (0x46a551)
Panic: panic(s)
/home/me/go/src/myapp/main.go:84 (0x4098bd)
GetUsers: log.Panic(err)
/home/me/go/src/github.com/gin-gonic/gin/context.go:97 (0x47773a)
(*Context).Next: c.handlers[c.index](c)
/home/me/go/src/myapp/main.go:54 (0x40b170)
Cors.func1: c.Next()
/home/me/go/src/github.com/gin-gonic/gin/context.go:97 (0x47773a)
(*Context).Next: c.handlers[c.index](c)
/home/me/go/src/github.com/gin-gonic/gin/recovery.go:45 (0x489fd1)
RecoveryWithWriter.func1: c.Next()
/home/me/go/src/github.com/gin-gonic/gin/context.go:97 (0x47773a)
(*Context).Next: c.handlers[c.index](c)
/home/me/go/src/github.com/gin-gonic/gin/logger.go:63 (0x48926a)
LoggerWithWriter.func1: c.Next()
/home/me/go/src/github.com/gin-gonic/gin/context.go:97 (0x47773a)
(*Context).Next: c.handlers[c.index](c)
/home/me/go/src/github.com/gin-gonic/gin/gin.go:284 (0x47e592)
(*Engine).handleHTTPRequest: context.Next()
/home/me/go/src/github.com/gin-gonic/gin/gin.go:265 (0x47e1c7)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/lib/go-1.6/src/net/http/server.go:2081 (0x60372e)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/lib/go-1.6/src/net/http/server.go:1472 (0x5fffde)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/lib/go-1.6/src/runtime/asm_amd64.s:1998 (0x467171)
goexit: BYTE $0x90 // NOP
What's wrong here? How can I fix it?
答案1
得分: 2
错误消息已经说得很清楚了:). Get() 方法不适用于扫描到切片中,请使用 Select() 方法代替。
英文:
Error message says pretty much everything :). Get() method is not intended to scan into slice, use Select() instead
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论