dortmundbvb0624 2019-08-20 22:35
浏览 109
已采纳

使用Go 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);
select @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:

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

I'm not sure why such a basic piece of SQL could be so problematic. Any insights anyone?

  • 写回答

1条回答 默认 最新

  • dougua2309 2019-08-21 00:32
    关注

    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.

    Call your stored procedure in one call to db.Query(), then query select @newargumentid in a second call to db.Query().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序