如何在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.
我通过在连接到 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