dortmundbvb0624 2019-08-20 14: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-20 16: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().

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部