go
data_error
ai_generated
true
sql: 第 1 列扫描错误:将 driver.Value 类型 string ("hello") 转换为 int:无效语法
sql: Scan error on column index 1: converting driver.Value type string ("hello") to a int: invalid syntax
ID: go/database-sql-rows-scan-type
80%修复率
87%置信度
0证据数
2024-02-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.16 | active | — | — | — |
| 1.20 | active | — | — | — |
根因分析
数据库返回的值无法在 Scan 期间转换为目标 Go 类型,通常是由于架构不匹配。
English
The database returned a value that cannot be converted to the target Go type during Scan, often due to schema mismatch.
解决方案
-
85% 成功率
Scan into an intermediate type and convert: var tmp string; rows.Scan(&tmp); val, _ := strconv.Atoi(tmp)
-
90% 成功率
Use sql.NullInt64 to handle nullable/invalid values: var n sql.NullInt64; rows.Scan(&n); if n.Valid { val := n.Int64 }
无效尝试
常见但无效的做法:
-
60% 失败
May not be feasible in production or requires migration.
-
80% 失败
Leads to incorrect data handling and potential bugs.