在Ruby中运行任意SQL查询

时间:2021-05-08 23:50:54

I've just been tasked with automating a reporting task at work. Previously, someone would run large, arbitrary SELECTs on a MySQL database using a GUI tool, then use that same tool to export the results to CSV. Now I want to write a Ruby script to do this.

我的任务是在工作中自动完成报告任务。以前,有人会使用GUI工具在MySQL数据库上运行任意选择,然后使用相同的工具将结果导出到CSV。现在我想编写一个Ruby脚本来实现这一点。

I know about FasterCSV in Ruby, but as far as SQL queries, I've only used ActiveRecord, where you're generally not writing the queries but using models and associations. The last time I wrote out complete SQL in code was when I coded PHP.

我知道Ruby中的FasterCSV,但就SQL查询而言,我只使用了ActiveRecord,通常不编写查询,而是使用模型和关联。我上一次用代码编写完整的SQL是在编写PHP时。

What's the most straightforward way to do this sort of thing in Ruby? Should I use ActiveRecord?

在Ruby中做这种事情最直接的方法是什么?我应该使用ActiveRecord吗?

3 个解决方案

#1


3  

If you are writing a complex application, there is absolutely nothing wrong with using ActiveRecord. Especially since you are already comfortable with it.

如果您正在编写一个复杂的应用程序,那么使用ActiveRecord绝对没有问题。尤其是你已经习惯了。

If you are writing a quick script and don't want to bother with ActiveRecord, you should also check out the mysql and mysql2 gems.

如果您正在编写一个快速脚本,并且不想使用ActiveRecord,那么还应该检查一下mysql和mysql2 gems。

#2


2  

I highly recommend Sequel. It has great documentation, active development, a thriving and helpful community, and it is (IMHO) simpler and better than ActiveRecord, especially for simple use cases like you appear to have.

我强烈推荐续集。它有很棒的文档、积极的开发、繁荣的和有用的社区,而且它(IMHO)比ActiveRecord更简单、更好,特别是对于像您这样的简单用例。

You might want to start by reading the README, Cheat Sheet and Sequel for SQL Users.

您可能想先阅读SQL用户的README、Cheat Sheet和Sequel。

#3


0  

I often find that in reporting engines you end up with complex queries processing large amounts of data and ORMs like ActiveRecord just don't cut it. Unless your reports are really simple, I think you'll want to use raw SQL (with a tool like Sequel) or call stored procedures.

我经常发现,在报表引擎中,您最终会遇到处理大量数据的复杂查询,而ActiveRecord这样的ORMs则不会中断查询。除非您的报告非常简单,否则我认为您需要使用原始SQL(使用Sequel这样的工具)或调用存储过程。

#1


3  

If you are writing a complex application, there is absolutely nothing wrong with using ActiveRecord. Especially since you are already comfortable with it.

如果您正在编写一个复杂的应用程序,那么使用ActiveRecord绝对没有问题。尤其是你已经习惯了。

If you are writing a quick script and don't want to bother with ActiveRecord, you should also check out the mysql and mysql2 gems.

如果您正在编写一个快速脚本,并且不想使用ActiveRecord,那么还应该检查一下mysql和mysql2 gems。

#2


2  

I highly recommend Sequel. It has great documentation, active development, a thriving and helpful community, and it is (IMHO) simpler and better than ActiveRecord, especially for simple use cases like you appear to have.

我强烈推荐续集。它有很棒的文档、积极的开发、繁荣的和有用的社区,而且它(IMHO)比ActiveRecord更简单、更好,特别是对于像您这样的简单用例。

You might want to start by reading the README, Cheat Sheet and Sequel for SQL Users.

您可能想先阅读SQL用户的README、Cheat Sheet和Sequel。

#3


0  

I often find that in reporting engines you end up with complex queries processing large amounts of data and ORMs like ActiveRecord just don't cut it. Unless your reports are really simple, I think you'll want to use raw SQL (with a tool like Sequel) or call stored procedures.

我经常发现,在报表引擎中,您最终会遇到处理大量数据的复杂查询,而ActiveRecord这样的ORMs则不会中断查询。除非您的报告非常简单,否则我认为您需要使用原始SQL(使用Sequel这样的工具)或调用存储过程。