# Ruby DBI

**URL:** <https://rubytalk.org/t/ruby-dbi/43258>\
**Category:** ruby-talk\
**Created:** [24 December 2007 14:35 UTC](https://rubytalk.org/t/ruby-dbi/43258 "2007-12-24T14:35:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praveen\_Ray](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@Praveen\_Ray](https://rubytalk.org/u/Praveen_Ray)\
**Post date:** [24 December 2007 14:35 UTC](https://rubytalk.org/t/ruby-dbi/43258/1 "2007-12-24T14:35:18Z")

</div>

Hi

What’s the best way to do raw SQL in Ruby? Something like Perl’s DBI. I  
see there is a [Ruby  
DBI](http://rubyforge.org/projects/ruby-dbi/) but it seems in beta. Can one use ActiveRecord to run arbitrary  
SQL statements? I’m not really interested in a full-fledged ORM - just  
a way to run SQL succinctly and easily.

Thanks

---

<div class="post-metadata">

**Author:** ![Jim\_Clark](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jim\_Clark](https://rubytalk.org/u/Jim_Clark)\
**Post date:** [24 December 2007 19:32 UTC](https://rubytalk.org/t/ruby-dbi/43258/2 "2007-12-24T19:32:17Z")

</div>

Praveen Ray wrote:

> Hi  
> What's the best way to do raw SQL in Ruby? Something like Perl's DBI. I see there is a Ruby DBI \<[http://rubyforge.org/projects/ruby-dbi/&gt](http://rubyforge.org/projects/ruby-dbi/&gt); but it seems in beta. Can one use ActiveRecord to run arbitrary SQL statements? I'm not really interested in a full-fledged ORM - just a way to run SQL succinctly and easily.
> 
> Thanks

I think you'll find that Ruby's DBI works very well and you should give it a try, especially if you don't want the overhead of Rails. I've used it a couple of times and found it to be very easy having used Perl's DBI for many years. Feel free to post your script and any problems if you need further help.

Regards,  
Jim

---

<div class="post-metadata">

**Author:** ![Michael\_Glaesemann](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Michael\_Glaesemann](https://rubytalk.org/u/Michael_Glaesemann)\
**Post date:** [24 December 2007 20:17 UTC](https://rubytalk.org/t/ruby-dbi/43258/3 "2007-12-24T20:17:25Z")

</div>

I've been happily using Ruby DBI as is. Of course, test it to make sure it suits your needs, but I doubt you'll have many problems with it.

Michael Glaesemann  
grzm seespotcode net

> **···**
>
> On Dec 24, 2007, at 9:35 , Praveen Ray wrote:
> 
> > What's the best way to do raw SQL in Ruby? Something like Perl's DBI. I see there is a Ruby DBI but it seems in beta.

---

<div class="post-metadata">

**Author:** ![Sharon\_Rosner](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Sharon\_Rosner](https://rubytalk.org/u/Sharon_Rosner)\
**Post date:** [24 December 2007 21:18 UTC](https://rubytalk.org/t/ruby-dbi/43258/4 "2007-12-24T21:18:32Z")

</div>

> What's the best way to do raw SQL in Ruby? Something like Perl's DBI. I see there is aRuby DBIbut it seems in beta. Can one

You can give Sequel a try. it provides a simple way to connect to  
databases, and you can run raw SQL statements:

&nbsp;&nbsp;require 'sequel'  
&nbsp;&nbsp;DB = Sequel('postgres://localhost/mydb')  
&nbsp;&nbsp;DB['select \* from my\_table'].each do |row|  
&nbsp;&nbsp;&nbsp;&nbsp;p row  
&nbsp;&nbsp;end

You can also specify your queries in Ruby:

&nbsp;&nbsp;DB[:items].filter {:category == 'ruby' && :price \<  
100}.order(:name).each do |row|  
&nbsp;&nbsp;&nbsp;&nbsp;p row  
&nbsp;&nbsp;end

More info here:

&nbsp;&nbsp;[Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/ruby-sequel)  
&nbsp;&nbsp;[http://sequel.rubyforge.org](http://sequel.rubyforge.org)  
&nbsp;&nbsp;[http://groups.google.com/group/sequel-talk](http://groups.google.com/group/sequel-talk)

---

<div class="post-metadata">

**Author:** ![Gerardo\_Santana\_Gome](https://avatars.discourse-cdn.com/v4/letter/g/b5ac83/32.png) [@Gerardo\_Santana\_Gome](https://rubytalk.org/u/Gerardo_Santana_Gome)\
**Post date:** [26 December 2007 05:14 UTC](https://rubytalk.org/t/ruby-dbi/43258/5 "2007-12-26T05:14:45Z")

</div>

Same problem I have. Ruby DBI doesn't \_look\_ finished so I'm trying some O/RMs.

We are using ActiveRecord at work to write database agnostic scripts  
and I'm testing Sequel, which looks pretty nice (  
[Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/ruby-sequel) ).

Other O/RMs:

\* datamapper: [http://www.datamapper.org/](http://www.datamapper.org/) (looks nice too)  
\* Og: [nitroproject.org](http://www.nitroproject.org/docs/rdoc/index.html)  
\* Kansas: [http://enigo.com/projects/kansas/index.html](http://enigo.com/projects/kansas/index.html)

> **···**
>
> On 12/24/07, Praveen Ray \<praveen@perspectivepartners.com\> wrote:
> 
> > Hi  
> > What's the best way to do raw SQL in Ruby? Something like Perl's DBI. I see  
> > there is a Ruby DBI but it seems in beta. Can one use ActiveRecord to run  
> > arbitrary SQL statements? I'm not really interested in a full-fledged ORM -  
> > just a way to run SQL succinctly and easily.
> > 
> > Thanks
> 
> --  
> Gerardo Santana

---

<div class="post-metadata">

**Author:** ![Joey\_Gibson](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Joey\_Gibson](https://rubytalk.org/u/Joey_Gibson)\
**Post date:** [26 December 2007 16:52 UTC](https://rubytalk.org/t/ruby-dbi/43258/6 "2007-12-26T16:52:38Z")

</div>

I've also been using Ruby DBI for years without issue. I've written hundreds  
of scripts using it and it's always worked a treat. I've used it with both  
MySQL and SQL Server (though that was a long time ago).

Joey

> **···**
>
> On Dec 24, 2007 3:17 PM, Michael Glaesemann \<grzm@seespotcode.net\> wrote:
> 
> > On Dec 24, 2007, at 9:35 , Praveen Ray wrote:
> > 
> > \> What's the best way to do raw SQL in Ruby? Something like Perl's  
> > \> DBI. I see there is a Ruby DBI but it seems in beta.
> > 
> > I've been happily using Ruby DBI as is. Of course, test it to make  
> > sure it suits your needs, but I doubt you'll have many problems with it.
> 
> --  
> Jobkabob: [http://www.jobkabob.com](http://www.jobkabob.com)
> 
> [http://www.joeygibson.com/blog](http://www.joeygibson.com/blog)
> 
> > **[Wisdom From Unexpected Places](https://joeygibson.com/2003/04/09/wisdom-from-unexpected-places/)**
> >
> > Last night I spent about three hours playing Rollercoaster Tycoon with my 4.5 year old son, Thomas. You can see a graphic of the theme park we built here (158KB). We were having a great time buildi…

---

<div class="post-metadata">

**Author:** ![Tim\_Uckun](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tim\_Uckun](https://rubytalk.org/u/Tim_Uckun)\
**Post date:** [1 January 2008 09:05 UTC](https://rubytalk.org/t/ruby-dbi/43258/7 "2008-01-01T09:05:46Z")

</div>

> \>
> 
> I've also been using Ruby DBI for years without issue. I've written hundreds  
> of scripts using it and it's always worked a treat. I've used it with both  
> MySQL and SQL Server (though that was a long time ago).

If you using SQL server don't rely on prepare statements to protect  
you from SQL injection attacks. All it's doing is simple string  
substitutions (it doesn't actually create prepared statements).

I had better luck with ODBC then ADO when going against SQL server  
FTIW. The ADO driver need some more work.
