go语言将表数据动态转成切片(字段任意拓展)
package main import ( "log" "fmt" "database/sql" _ "github.com/go-sql-driver/mysql" ) //先把字段的值都当成字符串 func Rows2SliceMap(rows *sql.Rows) (list []map[string]string) { //字段名称 columns, _ := rows.Columns() //多少个字段 length := len(columns) //每一行字段的值 values := make([]sql.RawBytes, length) //保存的是values的内存地址 pointer := make([]interface{}, length) // for i := 0; i < length; i++ { pointer[i] = &values[i] } // for rows.Next() { //把参数展开,把每一行的值存到指定的内存地址去,循环覆盖,values也就跟着被赋值了 rows.Scan(pointer...) //每一行 row := make(map[string]string) for i := 0; i < length; i++ { row[columns[i]] = string(values[i]) } list = append(list, row) } // return } func main() { db, err := sql.Open("mysql", "root:1234@tcp(192.168.99.165:3306)/test?charset=utf8") if err != nil { log.Println(err.Error()) } err = db.Ping() if err != nil { log.Println(err.Error()) } rows, _ := db.Query("select * from cms") defer rows.Close() list := Rows2SliceMap(rows) for k, v := range list { fmt.Println(k) fmt.Println(v["id"], v["name"], v["sex"], v["age"]) } }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25