英文:
Stored procedures with ODBC through Go/Golang
问题
我正在使用The Brainman的ODBC驱动程序。我正在使用通用的ODBC语法,即使用"CALL"来调用存储过程。以下是我的代码:
stmt, stmtErr := db.Prepare("CALL RecordClick (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
if stmtErr != nil {
fmt.Printf("\nstmtErr: %s", stmtErr)
}
defer stmt.Close()
var aclickid int
stmtRows, stmtRowsErr := stmt.Query(xaid, subtag, r.Referer, requestUserAgent, requestIP, ip, ua, title, description, displayurl, clickUrl, kw, rpc, exid)
if stmtRowsErr != nil {
fmt.Printf("\nstmtRowsErr: %s", stmtRowsErr)
}
for stmtRows.Next() {
stmtRows.Scan(&aclickid)
}
当我运行这段代码时,我得到以下错误:
stmtRowsErr: sql: converting Exec argument #2's type: unsupported type func() string, a func
我真的不明白我在这里做错了什么。我已经尝试过在"CALL XXX (?, ?, ?)"部分周围加上花括号,但仍然无法工作。有什么想法吗?
谢谢。
英文:
I'm using the ODBC driver from The Brainman. I'm using the generic ODBC syntax, which is to use "CALL" to invoke a Stored Procedure. Here is my code:
stmt, stmtErr := db.Prepare("CALL RecordClick (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
if stmtErr != nil {
fmt.Printf("\nstmtErr: %s", stmtErr)
}
defer stmt.Close()
var aclickid int
stmtRows, stmtRowsErr := stmt.Query(xaid, subtag, r.Referer, requestUserAgent, requestIP, ip, ua, title, description, displayurl, clickUrl, kw, rpc, exid)
if stmtRowsErr != nil {
fmt.Printf("\nstmtRowsErr: %s", stmtRowsErr)
}
for stmtRows.Next() {
stmtRows.Scan(&aclickid)
}
When I run this, I get the following error:
stmtRowsErr: sql: converting Exec argument #2's type: unsupported type func() string, a func
I really don't understand what I'm doing wrong here. I've tried it with and without curly braces around the CALL XXX (?, ?, ?) part, and it still won't work. Any ideas?
Thanks.
答案1
得分: 2
我的猜测是,在stmt.Query
中,r.Referer
(类型为func
)应该是r.Referer()
(返回类型为string
,来自net/http
)。
所以这一行应该改为:
stmtRows, stmtRowsErr := stmt.Query(xaid, subtag, r.Referer(), requestUserAgent, requestIP, ip, ua, title, description, displayurl, clickUrl, kw, rpc, exid)
该错误来自database/sql
包中的convert.go
文件的第37行。
它告诉你,SQL查询的第2个参数(从0开始计数)的类型转换无法转换为SQL引擎所期望的类型(特别是函数driver.DefaultParameterConverter.ConvertValue(arg)
失败)。
使用以下代码检查查询中每个变量的类型:
fmt.Printf("%T %T %T %T %T %T %T %T %T %T %T %T %T %T", xaid, subtag, r.Referer, requestUserAgent, requestIP, ip, ua, title, description, displayurl, clickUrl, kw, rpc, exid)
看看它们中是否有意外或不正确的类型。
英文:
My guess is that r.Referer
(Type func
) in stmt.Query
should be r.Referer()
(Returns type string
, From net/http
).
So the line should read:
stmtRows, stmtRowsErr := stmt.Query(xaid, subtag, r.Referer(), requestUserAgent, requestIP, ip, ua, title, description, displayurl, clickUrl, kw, rpc, exid)
That error is coming from Line #37 of convert.go
in the database/sql
package.
It's telling you that the type conversion for argument #2 (zero indexed) of the SQL query couldn't be converted into the type expected by the SQL engine (In particular the function driver.DefaultParameterConverter.ConvertValue(arg)
failed.)
Check the types for each variable in your query with something like:
fmt.Printf("%T %T %T %T %T %T %T %T %T %T %T %T %T %T", xaid, subtag, r.Referer, requestUserAgent, requestIP, ip, ua, title, description, displayurl, clickUrl, kw, rpc, exid)
and see if any of them are unexpected or incorrect.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论