# Ruby DBI

**URL:** <https://www.ruby-forum.com/t/ruby-dbi/124088>\
**Category:** Ruby\
**Created:** [December 24, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/ruby-dbi/124088 "2007-12-24T15:36:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praveen\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Praveen\_R](https://www.ruby-forum.com/u/Praveen_R)\
**Post date:** [December 24, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/ruby-dbi/124088/1 "2007-12-24T15:36:33Z")

</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:** ![Praveen\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Praveen\_R](https://www.ruby-forum.com/u/Praveen_R)\
**Post date:** [December 24, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/ruby-dbi/124088/2 "2007-12-24T20:33:20Z")

</div>

Praveen R. 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/](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  
> 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:** ![Praveen\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Praveen\_R](https://www.ruby-forum.com/u/Praveen_R)\
**Post date:** [December 24, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/ruby-dbi/124088/3 "2007-12-24T22:20:58Z")

</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:

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

You can also specify your queries in Ruby:

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

More info here:

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

---

<div class="post-metadata">

**Author:** ![Praveen\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Praveen\_R](https://www.ruby-forum.com/u/Praveen_R)\
**Post date:** [January 1, 2008, 10:06am UTC](https://www.ruby-forum.com/t/ruby-dbi/124088/4 "2008-01-01T10:06:26Z")

</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.

---

<div class="post-metadata">

**Author:** ![Praveen\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Praveen\_R](https://www.ruby-forum.com/u/Praveen_R)\
**Post date:** [December 26, 2007, 6:15am UTC](https://www.ruby-forum.com/t/ruby-dbi/124088/5 "2007-12-26T06:15:13Z")

</div>

On 12/24/07, Praveen R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

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)

---

<div class="post-metadata">

**Author:** ![Praveen\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Praveen\_R](https://www.ruby-forum.com/u/Praveen_R)\
**Post date:** [December 26, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/ruby-dbi/124088/6 "2007-12-26T17:53:31Z")

</div>

On Dec 24, 2007 3:17 PM, Michael G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Dec 24, 2007, at 9:35 , Praveen R. 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.

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

–  
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:** ![Praveen\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Praveen\_R](https://www.ruby-forum.com/u/Praveen_R)\
**Post date:** [December 24, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/ruby-dbi/124088/7 "2007-12-24T21:18:19Z")

</div>

On Dec 24, 2007, at 9:35 , Praveen R. 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.

Michael G.  
grzm seespotcode net
