Go, pgx: SELECT查询只返回一行

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

Go, pgx: SELECT query returns only one row

问题

Golang,pgx:
我正在尝试从t_example表中获取所有行(目前有20个项目),但是由于某种原因只返回一个(第一个)。我尝试进行调试,发现在第一次迭代后,rows.Next()返回false。
你能帮我提供一些建议吗?

我是个新手,但我已经提前在这里寻找了类似的情况 Go, pgx: SELECT查询只返回一行

我的代码:

func (ts *exampleStorage) GetAll() *[]Example {
q := `SELECT id, name FROM t_example`

rows := ts.client.Query(context.Background(), q)

example := make([]Example, 0)

for rows.Next() {
	var ex Example
	rows.Scan(&ex.Id, &ex.Name)
	example = append(example, ex)
}

return &example
}
英文:

Golang, pgx:
I am trying to get all rows from t_example (currently 20 items), however for some reason only one returns (the first one). I tried to debug and rows.Next() returns false after the first iteration.
Could you please help me with advice?

I'm a newbie, but I've tried to find similar cases here in advance Go, pgx: SELECT查询只返回一行

My code:

func (ts *exampleStorage) GetAll() *[]Example {
q := `SELECT id, name FROM t_example`

rows := ts.client.Query(context.Background(), q)

example := make([]Example, 0)

for rows.Next() {
	var ex Example
	rows.Scan(&ex.Id, &ex.Name)
	example = append(example, ex)
}

return &example
}

答案1

得分: 2

你的代码没有检查错误:

  • row.Scan(&ex.Id, &ex.Name) 可能会返回一个错误(在 pgx 实现中,这个错误对于 rows 迭代是致命的):
    err := rows.Scan(&ex.Id, &ex.Name)
    if err != nil {
        fmt.Printf("*** rows.Scan error: %s", err)
        return nil, err
    }
  • sql.Rows / pgx.Rows 的错误检查中有一个陷阱:你应该在退出 for rows.Next() { 循环之后检查是否发生了错误:

for rows.Next() {
    ...
}
// 在最后一个 rows.Next() 之后检查 rows.Err():
if err := rows.Err(); err != nil {
    // 除了在 'rows.Scan()' 调用中触发的错误之外,
    // 还可能发生一些不好的情况,比如由于网络错误导致的响应截断等等...
    fmt.Printf("*** iteration error: %s", err)
    return nil, err
}

return example, nil

一个附注:在绝大多数情况下,你不应该返回一个切片的指针(例如:*[]Example),而是返回一个切片(例如:[]Example)。

英文:

Your code doesn't check for errors :

  • row.Scan(&ex.Id, &ex.Name) could return an error (and, in pgx implementation, this error is fatal for the rows iteration) :
    err := rows.Scan(&ex.Id, &ex.Name)
    if err != nil {
        fmt.Printf("*** rows.Scan error: %s", err)
        return nil, err
    }
  • there is a gotcha with sql.Rows / pgx.Rows error checking : you should check if an error occurred after exiting the for rows.Next() { loop :

for rows.Next() {
    ...
}
// check rows.Err() after the last rows.Next() :
if err := rows.Err(); err != nil {
    // on top of errors triggered by bad conditions on the 'rows.Scan()' call,
    // there could also be some bad things like a truncated response because
    // of some network error, etc ...
    fmt.Printf("*** iteration error: %s", err)
    return nil, err
}

return example, nil

a side note : in the vast majority of cases you don't want to return a pointer to a slice (e.g: *[]Example) but a slice (e.g: []Example)

huangapple
  • 本文由 发表于 2022年5月27日 01:35:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/72395876.html
匿名

发表评论

匿名网友

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

确定