How does one exec sql scripts with Go?

blov · · 612 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Need to execute huge scripts like that found at <a href="https://github.com/unee-t/bz-database/tree/master/scripts" rel="nofollow">https://github.com/unee-t/bz-database/tree/master/scripts</a> I filed a (closed) bug here <a href="https://github.com/go-sql-driver/mysql/issues/789" rel="nofollow">https://github.com/go-sql-driver/mysql/issues/789</a> Currently it appears I need to manually rewrite the scripts which is a gigantic time sink!</p> <hr/>**评论:**<br/><br/>SeerUD: <pre><p>This is a great place to start: <a href="http://go-database-sql.org/" rel="nofollow">http://go-database-sql.org/</a></p> <p>From there, just read the SQL files using <code>os.Open</code>, read all of the SQL from it, and then run it like you would any other query.</p></pre>icholy: <pre><p>You can always just execute the mysql cli tools using os/exec</p> <p>edit: this is the dns param you&#39;re looking for <a href="https://github.com/go-sql-driver/mysql#multistatements" rel="nofollow">https://github.com/go-sql-driver/mysql#multistatements</a></p></pre>kai: <pre><p>thanks, though even with multistatements enabled, I&#39;m a little confused how SQL scripts with backticks get escaped.</p></pre>icholy: <pre><p>What are you talking about? An SQL &#34;script&#34; is just a query in a file.</p></pre>kai: <pre><p>So you&#39;re right. Reading in the file <code>ioutil.ReadFile(&#34;sql/script.sql&#34;)</code> and executing as a string seems work with the DNS option <code>?multiStatements=true</code> configured.</p></pre>icholy: <pre><p>I think you meant to say &#34;thank you for doing my job for me&#34;</p></pre>kai: <pre><p>Thanks for being patronising! It&#39;s DSN btw, not dns. Thankfully we don&#39;t work together.</p></pre>icholy: <pre><p>You&#39;re an incompetent piece of shit</p></pre>dgryski: <pre><p>Well, that escalated quickly..</p> <p>Please reconsider your wording.</p></pre>kjwabn: <pre><p>you can use xorm. <a href="http://xorm.io/" rel="nofollow">http://xorm.io/</a></p></pre>kai: <pre><p>I don&#39;t see a function to run a script...</p></pre>Gigaftp: <pre><p>Not hard to implement. Read the file in as a string, ex3cute query string. Profit.</p></pre>

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

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