Ruby DBI

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

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; 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

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.

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://sequel.rubyforge.org
  http://groups.google.com/group/sequel-talk

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

Other O/RMs:

* datamapper: http://www.datamapper.org/ (looks nice too)
* Og: nitroproject.org
* Kansas: 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

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.joeygibson.com/blog

>

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.