如何在IRB中的Mysql2 Gem中通过一次调用运行多个sql查询?
我正在使用 mysql2 (和 mysql 5)gem 及其结果和可枚举结果。
我可以运行诸如此类的查询
results = client.query("select now()")
我也可以运行诸如此类的查询
results = client.query("select version()")
但是我想做的就是将其提升一个档次。在现实生活中,我假设人们运行多个查询。那么我如何确保我可以一次性获得版本和时间。
--
我尝试过的事情不起作用:
results = client.query("select version(); select now()")
我得到的错误是:
Mysql2::Error: You have an error in your SQL Syntax; check the manual that corresponds to your Mysql Version for the right syntax to use near 'select now()' at line1
现在我明白我可以在 Mysql 控制台中运行以下查询并获取结果,我将如何做同样的事情Mysql2 Gem 中的事情:
select version();select now()
我如何在一个命令行中对 Mysql2 gem 做同样的事情(或者我需要两个)。我问这个问题是因为在现实生活中,人们通常会运行多个查询以按照他们想要的方式获得结果。
I am playing around with mysql2 ( and mysql 5) gem and the results and the Enumerable results.
I can run queries such as
results = client.query("select now()")
And I can also run queries such as
results = client.query("select version()")
But what I want to do is take it up a notch. In real life, I assume people run multiple queries. So how would I make sure that I can get the version and the time in one shot.
--
Things that I have tried that do not work:
results = client.query("select version(); select now()")
The error I get is:
Mysql2::Error: You have an error in your SQL Syntax; check the manual that corresponds to your Mysql Version for the right syntax to use near 'select now()' at line1
Now I understand that I can run the following queries in Mysql Console and get the results back, how would I do the same thing in Mysql2 Gem:
select version();select now()
How would I do the same with Mysql2 gem in that one command line(or do I need two). I ask because in real life, people usually run multiple queries to get the results in the way they want them in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过在连接到 MySQL 之前添加此默认查询选项解决了这个问题:
Mysql2::Client.default_query_options[:connect_flags] |= Mysql2::Client::MULTI_STATEMENTS
I solved this issue by adding this default query option before connecting to MySQL :
Mysql2::Client.default_query_options[:connect_flags] |= Mysql2::Client::MULTI_STATEMENTS
您可以使用 a 一次选择多个内容,如下所示:
如果您需要将它们放入一个结果中,则可以使用以下方法:
You can just select multiple things at once using a , as in:
If you need them into one result, then the following will work: