英文:
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
when inserting new row.
exit status 1
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论