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

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:

http://code.google.com/p/ruby-sequel
http://sequel.rubyforge.org
http://groups.google.com/group/sequel-talk

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.

On 12/24/07, Praveen R. [email protected] 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 (
http://code.google.com/p/ruby-sequel ).

Other O/RMs:

On Dec 24, 2007 3:17 PM, Michael G. [email protected]
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.joeygibson.com/blog
http://www.joeygibson.com/blog/life/Wisdom.html

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs