Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []driver.Named
na := s.NumInput()
if len(args)-start < na {
s.Close()
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args)-start)
}
stmtArgs := stmtArgs(args, start, na)
res, err = s.(*SQLiteStmt).exec(ctx, stmtArgs)
Expand Down
32 changes: 32 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,38 @@ func TestExecer(t *testing.T) {
}
}

func TestExecNotEnoughArgsErrorMessage(t *testing.T) {
tempFilename := TempFilename(t)
defer os.Remove(tempFilename)
db, err := sql.Open("sqlite3", tempFilename)
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer db.Close()

// Second statement is missing its arg. The "got" count should reflect
// the args remaining after the first statement consumed one, not the
// total number of args originally passed in.
_, err = db.Exec("SELECT ?; SELECT ?", "hello")
if err == nil {
t.Fatal("expected error for missing arg")
}
want := "not enough args to execute query: want 1 got 0"
if err.Error() != want {
t.Errorf("Exec error message:\n got: %q\nwant: %q", err.Error(), want)
}

// Query already reports the right number; check it stays that way so
// the two paths don't drift again.
_, err = db.Query("SELECT ?; SELECT ?", "hello")
if err == nil {
t.Fatal("expected error for missing arg")
}
if err.Error() != want {
t.Errorf("Query error message:\n got: %q\nwant: %q", err.Error(), want)
}
}

func TestQueryer(t *testing.T) {
tempFilename := TempFilename(t)
defer os.Remove(tempFilename)
Expand Down