go
data_error
ai_generated
true
sql:结果集中没有行
sql: no rows in result set
ID: go/sql-no-rows-in-result-set
80%修复率
89%置信度
0证据数
2024-02-27首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
QueryRow.Scan 未找到匹配行。代码将 sql.ErrNoRows 当作致命错误而非正常的未找到情形。
English
QueryRow.Scan found no matching row. Code treats sql.ErrNoRows as a fatal error instead of a normal not-found case.
解决方案
-
93% 成功率
err := db.QueryRowContext(ctx, q, id).Scan(&u.Name) if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } return &u, err -
82% 成功率
Use SELECT COUNT(*) first when existence matters, or return a typed ErrNotFound.
无效尝试
常见但无效的做法:
-
70% 失败
Still returns zero rows; the error handling issue remains.
-
85% 失败
Does not change the no-rows case.