英文:
Getting different results with bindvars in db.Query
问题
go版本:18.3
github.com/go-sql-driver/mysql v1.6.0
当查询字符串为
db.Query("SELECT * FROM logs ORDER BY log_id DESC LIMIT 1,3")
如果log_id == 8
b, _ := rawValue.([]byte)
下面是vscode的调试信息,b是正确的
b: []uint8 len:1,cap 1,[56] ; string(): "8"
当我使用
db.Query("SELECT * FROM logs ORDER BY log_id DESC LIMIT ?,?", 1,3)
如果log_id == 8
在b, _ := rawValue.([]byte)
中
信息是
b: []uint8 len :0 ,cap:0 ,nil
rawValue是8
而不是56
而且user(string)在两种情况下都正常工作。
我的SQL模式
CREATE TABLE logs (
`log_id` INT(6) NOT NULL AUTO_INCREMENT,
`user` VARCHAR(10),
PRIMARY KEY(log_id)
);
代码
db, _:= sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/log")
rows, _ := db.Query("SELECT * FROM logs ORDER BY log_id DESC LIMIT ?,?", 1, 3)
col, _ := rows.Columns()
defer rows.Close()
count := len(col)
values := make([]interface{}, count)
for i := range values {
var ii interface{}
values[i] = &ii
}
ret := make([]map[string]interface{}, 0)
fmt.Println("Reading data:")
for rows.Next() {
_ = rows.Scan(values...)
m := make(map[string]interface{}, 0)
for i, colname := range col {
rawValue := *(values[i].(*interface{}))
b, _ := rawValue.([]byte)
if colname == "log_id" {
v, _ := strconv.Atoi(string(b))
m[colname] = v
} else if colname == "log_msg_id" {
v, _ := strconv.Atoi(string(b))
m[colname] = v
} else {
v := string(b)
m[colname] = v
}
}
英文:
go version : 18.3
github.com/go-sql-driver/mysql v1.6.0
When Query string is
db.Query("SELECT * FROM logs ORDER BY log_id DESC LIMIT 1,3 ")
if the log_id == 8
b, _ := rawValue.([]byte)
Below is the vscode debug information,b is correct
b: []uint8 len:1,cap 1,[56] ; string():"8"
when i use
db.Query("SELECT * FROM logs ORDER BY log_id DESC LIMIT ?,? ", 1,3)
if the log_id == 8
In b, _ := rawValue.([]byte)
the info is
b: []uint8 len :0 ,cap:0 ,nil
rawValue is 8
not 56
and user(string) is work-well in the both.
My SQL schema
CREATE TABLE logs (
`log_id` INT(6) NOT NULL AUTO_INCREMENT,
`user` VARCHAR(10),
PRIMARY KEY(log_id)
);
code
db, _:= sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/log")
rows, _ := db.Query("SELECT * FROM logs ORDER BY log_id DESC LIMIT ?,? ", 1, 3)
col, _ := rows.Columns()
defer rows.Close()
count := len(col)
values := make([]interface{}, count)
for i := range values {
var ii interface{}
values[i] = &ii
}
ret := make([]map[string]interface{}, 0)
fmt.Println("Reading data:")
for rows.Next() {
_ = rows.Scan(values...)
m := make(map[string]interface{}, 0)
for i, colname := range col {
rawValue := *(values[i].(*interface{}))
b, _ := rawValue.([]byte)
if colname == "log_id" {
v, _ := strconv.Atoi(string(b))
m[colname] = v
} else if colname == "log_msg_id" {
v, _ := strconv.Atoi(string(b))
m[colname] = v
} else {
v := string(b)
m[colname] = v
}
}
答案1
得分: 0
为什么不直接将值扫描到这样的结构体中:
type Log struct {
ID int64
Name string
}
db, _ := sql.Open("mysql", "root:passwd@tcp(127.0.0.1:3306)/log")
rows, err := db.Query("SELECT * FROM logs ORDER BY log_id DESC LIMIT ?,?", 1, 3)
if err != nil {
fmt.Println(err)
}
defer rows.Close()
var values []Log
fmt.Println("读取数据:")
for rows.Next() {
var log Log
err = rows.Scan(&log.ID, &log.Name)
if err != nil {
fmt.Println(err)
continue // 需要处理错误
}
values = append(values, log)
}
fmt.Println(values)
英文:
Why not just scan the values to a struct like this:
type Log struct {
ID int64
Name string
}
db, _ := sql.Open("mysql", "root:passwd@tcp(127.0.0.1:3306)/log")
rows, err := db.Query("SELECT * FROM logs ORDER BY log_id DESC LIMIT ?,? ", 1, 3)
if err != nil {
fmt.Println(err)
}
defer rows.Close()
var values []Log
fmt.Println("Reading data:")
for rows.Next() {
var log Log
err = rows.Scan(&log.ID, &log.Name)
if err != nil {
fmt.Println(err)
continue // you need to handle error
}
values = append(values, log)
}
fmt.Println(values)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论