go data_error ai_generated true

sql: 扫描第 1 列错误:将 driver.Value 类型 []uint8 ("123") 转换为 int64 时语法无效

sql: Scan error on column index 1: converting driver.Value type []uint8 ("123") to a int64: invalid syntax

ID: go/sql-scan-type-mismatch

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-06-18首次发现

版本兼容性

版本状态引入弃用备注
1.21 active

根因分析

数据库返回数字的字符串表示,但目标类型是 int,转换失败。

English

Database returns a string representation of a number, but target is int; conversion fails.

generic

解决方案

  1. 95% 成功率
    Scan into a string first, then parse: var s string; rows.Scan(&s); num, _ := strconv.Atoi(s)
  2. 90% 成功率
    Use sql.NullInt64 and handle invalid: var n sql.NullInt64; rows.Scan(&n); if n.Valid { use n.Int64 }

无效尝试

常见但无效的做法:

  1. 40% 失败

    May not be possible or desirable; schema changes risky.

  2. 90% 失败

    Data loss; incorrect results.