~~~
func ExecuteSqlRs(sqls string) []string {
aaa := make([]string, 0)
row, err := mysql.DB.Query(sqls)
if err != nil {
return aaa
}
defer row.Close()
columns, err := row.Columns()
count := len(columns)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
a := make([]string, 0)
for row.Next() {
for i := 0; i < count; i++ {
valuePtrs[i] = &values[i]
row.Scan(valuePtrs...)
var v interface{}
val := values[i]
b, ok := val.([]byte)
if ok {
v = string(b)
} else {
v = val
}
text3 := v.(string)
a = append(a, text3)
}
}
return a
}
我想问下外部如何类似这样一行行取出呢
如:
text := ExecuteSqlRs("select id,username,from user where 1=1")
if len(text)>0{
for i :=0;i<len(text);i++{
//循环读出全部数据
fmt.Print("ID=",text[i]," 用户名",text[i])
}
}
这样读出的数据都是乱的。 我想循环读到全部数据,封装省去了写一堆代码,
~~~