go
data_error
ai_generated
true
error: sql: expected 1 argument, got 2
ID: go/database-sql-prepare-statement-error
80%Fix Rate
85%Confidence
0Evidence
2024-10-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
The number of arguments provided to a prepared statement does not match the number of placeholders in the query.
generic中文
提供给预编译语句的参数数量与查询中的占位符数量不匹配。
Workarounds
-
90% success Use named parameters with sql.Named
stmt, err := db.PrepareContext(ctx, "INSERT INTO users(name, age) VALUES(@name, @age)") if err != nil { return err } _, err = stmt.ExecContext(ctx, sql.Named("name", "Alice"), sql.Named("age", 30))
Dead Ends
Common approaches that don't work:
-
Counting placeholders manually
70% fail
Error-prone for complex queries; use named parameters or helper functions.