如何在IRB中的Mysql2 Gem中通过一次调用运行多个sql查询?

发布于 2024-10-15 07:35:52 字数 844 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2024-10-22 07:35:52

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

后来的我们 2024-10-22 07:35:52

您可以使用 a 一次选择多个内容,如下所示:

SELECT VERSION(), NOW();

如果您需要将它们放入一个结果中,则可以使用以下方法:

SELECT CONCAT(VERSION(), NOW());

You can just select multiple things at once using a , as in:

SELECT VERSION(), NOW();

If you need them into one result, then the following will work:

SELECT CONCAT(VERSION(), NOW());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文