Monday, February 8, 2010

Resolved: "uninitialized constant DBI::DBD::ODBC" in Ruby for Windows

  • Windows 2003 Server SP2
  • Ruby 1.8.6 patchlevel 383, i386-mingw32 (from RubyInstaller)
  • dbd-odbc 0.2.4
  • dbi 0.4.3
I created a Ruby class that can run arbitrary SQL queries on remote databases of various kinds. It was working fine for a while, but then something changed so that whenever I would try to use it to execute some SQL, I'd get the following error:

DBI::InterfaceError (Unable to load driver 'ODBC' (underlying error: uninitialized constant DBI::DBD::ODBC)):
dbi (0.4.3) lib/dbi.rb:300:in `load_driver'
C:/Ruby/lib/ruby/1.8/monitor.rb:242:in `synchronize'
dbi (0.4.3) lib/dbi.rb:242:in `load_driver'
dbi (0.4.3) lib/dbi.rb:160:in `_get_full_driver'
dbi (0.4.3) lib/dbi.rb:145:in `connect'

Make sure you have the dbd-odbc and dbi gems installed correctly.
For me, a couple of associated files were missing from C:\Ruby\lib\ruby\1.8\i386-mingw32: and I had these files in my working Dev installation, so I just copied them from Dev to Prod, and it fixed the problem.

This comes from DBI trying to do a require dbd/odbc and not finding that file. When I googled around about the error, mostly people said to make sure you had the dbd-odbc gem installed, which I already did. I tried reinstalling the gem, as well as the dbi gem. This had no effect. The problem was happening on Prod, but I had the same thing set up on Dev, which was working. So, I checked to see what paths require uses like so:

irb(main):001:0> $LOAD_PATH
=> ["C:/Ruby/lib/ruby/site_ruby/1.8", "C:/Ruby/lib/ruby/site_ruby/1.8/i386-msvcr
t", "C:/Ruby/lib/ruby/site_ruby", "C:/Ruby/lib/ruby/1.8", "C:/Ruby/lib/ruby/1.8/
i386-mingw32", "."]

I checked through those directories to see what .rb files looked like ODBC-related things that were in Dev but not in Prod. That's how I found those two files. After I copied them to Prod, I tested it in IRB like so:

irb(main):004:0> require 'odbc'
=> true

After that, everything worked fine again. I hope this anecdote can help you! Good luck!


Chuck said...

For any of you with this problem of the missing files, but don't happen to have a copy of them handy, another blogger describes not only this situation, but ALSO includes a link to where you can download the missing files


Luannd said...

you need to copy a new version of libMySql.dll into the Ruby\bin directory. The best source for that dll is here.

Kiều Dylan said...
This comment has been removed by the author.
Kiều Dylan said...

I'm new in ruby programming. I found the two files driver ( and but I dont know where is the Prod folder mentioned in your post? Could you help me point out the path exactly?

Thanks in advance!

Matthew Pistella said...

I think he meant he has ruby installed on two different server one is for testing and development and the other is Production or prod. If you found the files then they are most likely in the correct folder. Where was the path that you found them at?

Krizzzn said...

I've resolved this problem by installing the ruby-odbc gem.

Y Sashi said...

On an old Windows server (2007?), I was getting the same error, even though my so files were in the correct directory.
The fix was to add:
require ''

before any code that pulls in the db libraries.

Hope this helps ...

Bob Smyph said...

These instructions do not seem to work for Ruby 1.9.3 on a windows7 machine. any help in that area?

dijikul said...

Bob, we're running into each other on various forums. I'm wondering if you've found a solution, yet?