BibleGateway.com Verse Of The Day

Tuesday, May 29, 2007

Hooking Up Ruby To Oracle

Well, I have to admit this one stumped me for a few days. Actually it stumped me one day, and I got pulled to work on actuall work stuff, and just got back to it a few days later.

I started by following the examples at http://www.oracle.com/technology/pub/articles/haefel-oracle-ruby.html
to get the Ruby Oracle libraries installed. That part was easy, download the OCI8 Ruby package at http://rubyforge.org/projects/ruby-oci8 and run it.

Then I whipped up a script to test the connection from an example I found online (forget the URL to reference -- sorry if I stole your crap, dude!) That script basically looks like this (with usernames and passwords changed to protect the innocent.

test-conn.rb


require 'oci8'

OCI8.new("user", "password", "oracle_sid").exec("select * from rep") do
|r| puts r.join(", ");
end



I was pumped! That seems easy, and then I can get out of the MySQL realm and start hooking up Rails to existing enterprise DB's and really show this stuff to be useful in our environment. Sorry MySQL fanatics, we are (mostly) an Oracle shop -- I have nothing against MySQL for my personal projects.

Anyways, she didn't work out so well. I figured it would work since I have Oracle installed on my PC, but got the following popup when I ran that script.

There was also a Ruby stack trace on the command line. Basically I did some Google'ing and found this posting (http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/238003) which at least gave me the clue I needed to look for OCI.dll on my system. My thought was it was getting confused between the Oracle 8 and Oracle 9 versions loaded on my machine.

Searching and searching, I don't have OCI.dll on my machine. Back to Google (a developer's best friend IMO, it always confuses me when other developers don't try this first when they get stumped) That's when I came across this page (http://www.dll-files.com/dllindex/dll-files.shtml?oci) that let me download OCI.dll. Dropped that in C:\WINNT\system32 (I'm on Windows 2000 at work, might be WINDOWS instead of WINNT for you), and re-ran the above test-conn.rb script.

IT WORKS!
Ruby on Rails on Oracle, here I come (again)

I'm guessing my next big hurdle (or potentially a roadblock instead of a hurdle) will be the lack of support for complex primary keys. Starting an application from scratch, you can design your database with that limitation in mind, but building applications for existing databases becomes another issue. I'm sure someone smarter than me has already addressed these concerns, so it might just be a matter of more Google searches.

No comments: