Skip to content

expected ... arguments, got ... when using db.Prepare() #1485

@AndreKR

Description

@AndreKR

Observed

The program below creates a table and tries to insert two rows into it. The first row is inserted using db.Exec() the second one using stmt, err := db.Prepare() + stmt.Exec().

The insert using db.Exec() works, the insert using stmt.Exec() returns an error: expected 3 arguments, got 2

Expected behaviour

I'd expect both inserts to work identically.

Code example

package main

import (
	"database/sql"
	"log"

	_ "github.com/ClickHouse/clickhouse-go/v2"
)

func main() {
	dsn := "https://clickhouse:443/test?secure=true&username=user&password=pass&debug=true"

	db, err := sql.Open("clickhouse", dsn)
	if err != nil {
		log.Fatalf("failed to connect: %v", err)
	}

	_, err = db.Exec(`DROP TABLE example`)
	if err != nil {
		log.Fatalf("failed to drop table: %v", err)
	}

	_, err = db.Exec(`CREATE TABLE example (
		  a String,
		  b Nullable(String),
		  c Nullable(String)
		) ENGINE = MergeTree PRIMARY KEY a
	`)
	if err != nil {
		log.Fatalf("failed to create table: %v", err)
	}


	// This works
	_, err = db.Exec(`INSERT INTO example(a, c) VALUES('foo','bar')`)
	if err != nil {
		log.Fatalf("exec failed: %w", err)
	}

	// This does not
	stmt, err := db.Prepare(`INSERT INTO example(a, c) VALUES(?,?)`)
	if err != nil {
		log.Fatalf("prepare failed: %w", err)
	}
	_, err = stmt.Exec([]interface{} {"foo", "bar"}...)
	if err != nil {
		log.Fatalf("execution failed: %w", err)
	}
}

Error log

[clickhouse-std][conn=0][clickhouse:443] [batch][exec] append error: clickhouse [Append]:  clickhouse: expected 3 arguments, got 2
2025/01/31 03:54:10 execution failed: %!w(*proto.BlockError=&{Append 0xc000030250 })
exit status 1

Details

Environment

  • clickhouse-go version: v2.30.1
  • Interface: database/sql compatible driver
  • Go version: go version go1.23.5 windows/amd64
  • Operating system: Windows 10 Pro
  • ClickHouse version: 24.12.3.47
  • Is it a ClickHouse Cloud? No.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions