doucan957495 2018-09-02 21:00
浏览 136
已采纳

使用Golang返回mysql过程

I'm having a problem getting the return of a procedure using golang. If I remove the parameters from the procedure the procedure is executed normally, however, when I need to get the return (OUT parameter) I can not get it and the procedure is not executed. To make the tests simpler and to get help, I created a simple procedure as below and I want to get the return of it.

Follow procedure below:


drop procedure if exists PESSOA_TESTE;

delimiter $$


create procedure PESSOA_TESTE(out psaida int)

begin

set psaida = 2;

end

$$

Now it follows the section of golang code that I'm using to try to get the value 2 specified in the procedure.

var GerenciaBD GERENCIABD
var PontoExecucao int

GerenciaBD.F_GERENCIABD_ABRIR_CONEXAO_MYSQL()

GerenciaBD.DataBase.ExecContext(context.TODO(),"call PESSOA_TESTE", sql.Named("psaida", sql.Out{Dest:&PontoExecucao}))

println(PontoExecucao)

When executing I have as answer 0 and not 2, which is the one specified in the procedure.

My golang version is at 1.10.

  • 写回答

1条回答 默认 最新

  • douduoting8408 2018-09-04 08:08
    关注

    You can see here that usage of out parameter is not yet implemented for https://github.com/go-sql-driver/mysql library.

    I'm not sure what you want to do with your code, but if you want to return only one value, you can do something like this:

    Stored procedure:

    drop procedure if exists PESSOA_TESTE;
    
    delimiter $$
    
    
    create procedure PESSOA_TESTE(IN psaida int)
    
    begin
    SET psaida = 2;
    select psaida;
    
    end
    
    $$
    

    Code:

       var GerenciaBD GERENCIABD
        var PontoExecucao int
    
        GerenciaBD.F_GERENCIABD_ABRIR_CONEXAO_MYSQL()
    
        GerenciaBD.DataBase.QueryRowContext(context.TODO(),"CALL PESSOA_TESTE(?)", PontoExecucao).Scan(&PontoExecucao)
    
        println(PontoExecucao)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置