查询语句:
```
rows, err := mysqlDB.Query("SELECT * FROM `article` where `id` = ? ", 32);
```
相应函数为:
```
// Query via pool
func (p *SQLConnPool) Query(queryStr string, args ...interface{}) ([]map[string]interface{}, error) {
rows, err := p.SQLDB.Query(queryStr, args...)
if err != nil {
log.Println(err)
return []map[string]interface{}{}, err
}
defer rows.Close()
columns, _ := rows.Columns()
scanArgs := make([]interface{}, len(columns))
values := make([]interface{}, len(columns))
for i := range values {
scanArgs[i] = &values[i]
}
rowsMap := make([]map[string]interface{}, 0, 10)
for rows.Next() {
//将行数据保存到record字典
err = rows.Scan(scanArgs...)
rowMap := make(map[string]interface{})
for i, col := range values {
if col != nil {
rowMap[columns[i]] = string(col.([]byte))
}
}
rowsMap = append(rowsMap, rowMap)
}
if err = rows.Err(); err != nil {
return []map[string]interface{}{}, err
}
return rowsMap, nil
}
```
报错信息:
```
interface conversion: interface {} is int64, not []uint8
/usr/local/go/src/runtime/panic.go:491 (0x42b512)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/local/go/src/runtime/iface.go:172 (0x40f9b6)
panicdottypeE: panic(&TypeAssertionError{iface.string(), haveString, want.string(), ""})
/root/go/src/github.com/fecshopsoft/golang-db/mysql/mysql.go:74 (0x92cda2)
(*SQLConnPool).Query: rowMap[columns[i]] = string(col.([]byte))
/root/go/src/github.com/fecshop/go_fec_api/handler/article.go:54 (0x92e475)
ArticleData.OneByUrlKey: _, _ = mysqlDB.Query("SELECT * FROM `article` where `id` = ? ", 32);
/root/go/src/github.com/fecshop/go_fec_api/router/router.go:42 (0x92fba3)
Begin.func2: handler.Article.OneByUrlKey(c, mysqlDB)
/root/go/src/github.com/gin-gonic/gin/context.go:107 (0x8e2812)
(*Context).Next: c.handlers[c.index](c)
/root/go/src/github.com/gin-gonic/gin/recovery.go:46 (0x8f4ab9)
RecoveryWithWriter.func1: c.Next()
/root/go/src/github.com/gin-gonic/gin/context.go:107 (0x8e2812)
(*Context).Next: c.handlers[c.index](c)
/root/go/src/github.com/gin-gonic/gin/logger.go:83 (0x8f3deb)
LoggerWithWriter.func1: c.Next()
/root/go/src/github.com/gin-gonic/gin/context.go:107 (0x8e2812)
(*Context).Next: c.handlers[c.index](c)
/root/go/src/github.com/gin-gonic/gin/gin.go:359 (0x8eb925)
(*Engine).handleHTTPRequest: c.Next()
/root/go/src/github.com/gin-gonic/gin/gin.go:326 (0x8eb0ba)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/go/src/net/http/server.go:2619 (0x664903)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/go/src/net/http/server.go:1801 (0x660a4c)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/go/src/runtime/asm_amd64.s:2337 (0x459790)
goexit: BYTE $0x90 // NOP
```
看报错信息明白个大概,但是不知道如何改,来消除报错。
这几行是关键代码,需要注意的是有些数据库驱动ColumnTypes()没有实现是用不了的
```
cols, err := rows.Columns() // 获取列名
ct, err := rows.ColumnTypes() // 获取列类型
// 关键点
arr := make([]interface{}, len(ct))
for i, v := range ct {
t := v.ScanType()
v := reflect.New(t).Interface()
arr[i] = v
fmt.Println(cols[i], t)
}
// 这时候arr是带有类型的
err = rows.Scan(arr...)
```
#9
更多评论
你阔以参考一下这个: https://github.com/zengming00/go-server-js/blob/master/test/sql.go
#1