通过Go/Golang使用ODBC的存储过程

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

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.

huangapple
  • 本文由 发表于 2013年11月6日 13:16:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/19804636.html
匿名

发表评论

匿名网友

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

确定