Golang SQL 错误,期望 0 个参数,实际得到 3 个。

huangapple go评论121阅读模式
英文:

Golang SQL error expected 0 arguments got 3

问题

我正在使用github.com/denisenkom/go-mssqldb库和驱动程序,但在插入新行时遇到错误sql: expected 0 arguments, got 3 exit status 1

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

// 使用命名参数执行非查询操作
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 using github.com/denisenkom/go-mssqldb library and driver but getting an error sql: expected 0 arguments, got 3
exit status 1
when inserting new row.

tsql := "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
}

答案1

得分: 2

这个问题可能与连接字符串中使用的驱动程序名称有关。
我尝试使用你的查询,记录被成功创建而没有任何错误。

我相信你目前在连接字符串中使用的是mssql;sql.Open("mssql", conn)(这个问题已经在https://github.com/denisenkom/go-mssqldb/issues/594#issuecomment-809922317中讨论过)。

如果你尝试将"mssql"替换为"sqlserver",问题应该就会解决。

英文:

This issue might be related to driver name used in the connection string.
I've tried the same query with yours, the record is created without any errors.

I believe that you are currently using mssql in connection string; sql.Open("mssql", conn) (This issue has already been discussed in https://github.com/denisenkom/go-mssqldb/issues/594#issuecomment-809922317)

If you try again by replacing "mssql" to "sqlserver", the problem should be solved.

huangapple
  • 本文由 发表于 2021年11月27日 16:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/70133186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定