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

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

Go, pgx: SELECT query returns only one row

问题

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

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

我的代码:

  1. func (ts *exampleStorage) GetAll() *[]Example {
  2. q := `SELECT id, name FROM t_example`
  3. rows := ts.client.Query(context.Background(), q)
  4. example := make([]Example, 0)
  5. for rows.Next() {
  6. var ex Example
  7. rows.Scan(&ex.Id, &ex.Name)
  8. example = append(example, ex)
  9. }
  10. return &example
  11. }
英文:

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:

  1. func (ts *exampleStorage) GetAll() *[]Example {
  2. q := `SELECT id, name FROM t_example`
  3. rows := ts.client.Query(context.Background(), q)
  4. example := make([]Example, 0)
  5. for rows.Next() {
  6. var ex Example
  7. rows.Scan(&ex.Id, &ex.Name)
  8. example = append(example, ex)
  9. }
  10. return &example
  11. }

答案1

得分: 2

你的代码没有检查错误:

  • row.Scan(&ex.Id, &ex.Name) 可能会返回一个错误(在 pgx 实现中,这个错误对于 rows 迭代是致命的):
  1. err := rows.Scan(&ex.Id, &ex.Name)
  2. if err != nil {
  3. fmt.Printf("*** rows.Scan error: %s", err)
  4. return nil, err
  5. }
  • sql.Rows / pgx.Rows 的错误检查中有一个陷阱:你应该在退出 for rows.Next() { 循环之后检查是否发生了错误:
  1. for rows.Next() {
  2. ...
  3. }
  4. // 在最后一个 rows.Next() 之后检查 rows.Err():
  5. if err := rows.Err(); err != nil {
  6. // 除了在 'rows.Scan()' 调用中触发的错误之外,
  7. // 还可能发生一些不好的情况,比如由于网络错误导致的响应截断等等...
  8. fmt.Printf("*** iteration error: %s", err)
  9. return nil, err
  10. }
  11. 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) :
  1. err := rows.Scan(&ex.Id, &ex.Name)
  2. if err != nil {
  3. fmt.Printf("*** rows.Scan error: %s", err)
  4. return nil, err
  5. }
  • 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 :
  1. for rows.Next() {
  2. ...
  3. }
  4. // check rows.Err() after the last rows.Next() :
  5. if err := rows.Err(); err != nil {
  6. // on top of errors triggered by bad conditions on the 'rows.Scan()' call,
  7. // there could also be some bad things like a truncated response because
  8. // of some network error, etc ...
  9. fmt.Printf("*** iteration error: %s", err)
  10. return nil, err
  11. }
  12. 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:

确定