无法使用Go MySQL检索输出参数 [英] Can't retrieve output parameter with Go MySQL
问题描述
我有一个存储过程,该存储过程会插入一个实体并返回其新的UUID,虽然我可以生成代码以创建正确的查询,但是使用 go-sql-driver/mysql
时它会出错.所以下面的代码...
I have a stored procedure which inserts an entity and returns its new UUID, and while I can generate the code to create the right query, it errors out when using go-sql-driver/mysql
. So the following code...
func test_insert() *sql.Rows {
db := openDbConnection()
defer db.Close()
results, err := db.Query("call saveargument(null, 'Test Argument', 'Test Argument', '1', null, null, null, 1, 'test_user', @newargumentid);\nselect @newargumentid;")
toolkit.HandleError(err)
return results
}
func openDbConnection() *sql.DB {
var db, err = sql.Open("mysql", getConnectionString(entities.GetConfig()))
toolkit.HandleError(err)
return db
}
...产生以下错误:
... produces the following error:
您的SQL语法有错误;检查手册对应于您的MySQL服务器版本以使用正确的语法在第2行的"select @newargumentid"附近
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select @newargumentid' at line 2
我不确定为什么这样一个基本的SQL可能会有这么大的问题.任何有见识的人吗?
I'm not sure why such a basic piece of SQL could be so problematic. Any insights anyone?
推荐答案
您不能在单个 db.Query()
调用中运行多个语句.所有编程语言中的大多数查询接口都是如此.
You can't run more than one statement in a single db.Query()
call. This is true of most query interfaces in all programming languages.
在对 db.Query()
的一次调用中调用您的存储过程,然后在对 db.Query()<<的第二次调用中查询
select @newargumentid
/code>.
Call your stored procedure in one call to db.Query()
, then query select @newargumentid
in a second call to db.Query()
.
这篇关于无法使用Go MySQL检索输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!