How does one exec sql scripts with Go?

blov · 2018-05-03 19:30:31 · 755 次点击    
这是一个分享于 2018-05-03 19:30:31 的资源,其中的信息可能已经有所发展或是发生改变。

Need to execute huge scripts like that found at https://github.com/unee-t/bz-database/tree/master/scripts I filed a (closed) bug here https://github.com/go-sql-driver/mysql/issues/789 Currently it appears I need to manually rewrite the scripts which is a gigantic time sink!


评论:

SeerUD:

This is a great place to start: http://go-database-sql.org/

From there, just read the SQL files using os.Open, read all of the SQL from it, and then run it like you would any other query.

icholy:

You can always just execute the mysql cli tools using os/exec

edit: this is the dns param you're looking for https://github.com/go-sql-driver/mysql#multistatements

kai:

thanks, though even with multistatements enabled, I'm a little confused how SQL scripts with backticks get escaped.

icholy:

What are you talking about? An SQL "script" is just a query in a file.

kai:

So you're right. Reading in the file ioutil.ReadFile("sql/script.sql") and executing as a string seems work with the DNS option ?multiStatements=true configured.

icholy:

I think you meant to say "thank you for doing my job for me"

kai:

Thanks for being patronising! It's DSN btw, not dns. Thankfully we don't work together.

icholy:

You're an incompetent piece of shit

dgryski:

Well, that escalated quickly..

Please reconsider your wording.

kjwabn:

you can use xorm. http://xorm.io/

kai:

I don't see a function to run a script...

Gigaftp:

Not hard to implement. Read the file in as a string, ex3cute query string. Profit.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

755 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传