在使用`db.Query`中的`bindvars`时得到了不同的结果。

huangapple go评论76阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2022年6月27日 15:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/72768134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定