Error creating new row

Hi,
I’m trying to insert a new row to MS SQL using denisenkom go-mssqldb package and driver



tsql := fmt.Sprintf("INSERT INTO Uploads (Fname, Fsize, Ftype) VALUES (@Fname, @Fsize, @Ftype );")
	fmt.Printf("tsql = %s\n", tsql)

	//Execute non-query with named parameters
	res, err := db.ExecContext(
		ctx,
		tsql,
		sql.Named("Fname", fname),
		sql.Named("Fsize", fsize),
		sql.Named("Ftype", ftype))

	if err != nil {
		log.Fatal(" AddRow_v1() -> Error creating new row: " + err.Error())
		return -1, err
	}

I’m getting unncessary use of fmt.Sprintf
Also I’m getting a terminal error sql: expected 0 arguments

You see this error because you’re using Sprintf without using any formatting placeholder or variable. Instead, just assign the raw string to tsql:

tsql := "INSERT INTO Uploads (Fname, Fsize, Ftype) VALUES (@Fname, @Fsize, @Ftype );"
1 Like

About error sql: expected 0 arguments, which SQL library do you use? Does it support named parameters?

I’m using github.com/denisenkom/go-mssqldb for Microsoft SQL Server 2014 - 12.0.2269.0 (X64) Jun 10 2015 03:35:45 Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows NT 6.3 <X64> (Build 19042: )

Which SQL library support named parameters?

I just had a quick look on mssql package - github.com/denisenkom/go-mssqldb - pkg.go.dev but could not spot the problem, maybe someone else can help here?

1 Like