go data_error ai_generated true

error: sql: expected 1 argument, got 2

ID: go/database-sql-prepare-statement-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-10-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 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:

  1. Counting placeholders manually 70% fail

    Error-prone for complex queries; use named parameters or helper functions.