dongzanxun2790 2018-07-30 17:04
浏览 168
已采纳

从sql文件加载存储过程

I have a mysql test.sql file that contains stored procedure. to load this sql file after connecting to database in go server, I used Exec command. But I haven't got the result I wanted. I take error code

1064 You have an error in your SQL syntax; check the manual that corr...

How can I load stored procedure from a sql file after connecting the database in go.

// go code section :

sqlProc, err := ioutil.ReadFile("E:/Qlass/goserv/src/cevir/test.sql")
// handle error

_, err = MAPP.DB.Db.Exec(string(sqlProc[:]))
// handle error

// content of test.sql

drop procedure if exists Test;
delimiter ;;
create procedure Test()
begin
    truncate table _prlog;
end ;;
delimiter ;

problem is caused by the delimeter command. I removed those lines. Problem is solved. The corrected sql file.

drop procedure if exists Test;
create procedure Test()
begin
    truncate table _prlog;
end ;
  • 写回答

1条回答 默认 最新

  • douningqiu4991 2018-07-30 17:25
    关注

    I would expect something more like this:

    DB, err = sql.Open("mysql", MAPP.CF.Mysql)
    if err != nil {
        // handle error
    }
    data, err := ioutil.ReadFile(`E:/Qlass/goserv/src/modul/modul_sp.sql`)
    if err != nil {
        // handle error
    }
    sqlProc := string(data)
    _, err := DB.Exec(sqlProc)
    if err != nil {
        // handle error
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?