去,PGX:选择查询仅返回一行

发布于 2025-02-01 10:38:08 字数 482 浏览 3 评论 0原文

Golang,PGX: 我正在尝试从t_example(目前20个项目)获取所有行,但是出于某种原因仅返回(第一个)。我尝试调试和rows.next()在第一次迭代后返回false。 您能帮我提供建议吗?

我是新手,但我试图提前在这里找到类似的案例:)

我的代码:

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 :)

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2025-02-08 10:38:08

您的代码未检查错误:

  • row.scan(& ex.id,ex.name)可能会返回错误(并且,在pgx instructation in offormant,此错误对于迭代是致命的):
    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(){ 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

旁注:在绝大多数情况下,您不想将指针返回到一个切片(例如:*[]示例),但是切片(例如:[]示例

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文