Read more

Fix [RubyODBC]Cannot allocate SQLHENV when connecting to MSSQL 2005 with Ruby 1.8.7. on Ubuntu 10.10

Deleted user #6
March 23, 2012Software engineer

I followed this nice guide Connecting to MSSQL with Ruby on Ubuntu - lambie.org until I ran in the following errors:

irb(main):001:0> require "dbi"; dbh = DBI.connect('dbi:ODBC:MyLegacyServer', 'my_name', 'my_password')

DBI::DatabaseError: INTERN (0) [RubyODBC]Cannot allocate SQLHENV
  from /usr/lib/ruby/1.8/dbd/odbc/driver.rb:36:in `connect'
  from /usr/lib/ruby/1.8/dbi/handles/driver.rb:33:in `connect'
  from /usr/lib/ruby/1.8/dbi.rb:148:in `connect'
  from (irb):1
  from /usr/local/lib/site_ruby/1.8/rubygems/dependency_list.rb:14
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

I installed the gem ruby-odbc Show archive.org snapshot instead of the guide's sudo aptitude install libdbd-odbc-ruby

sudo gem install ruby-odbc #Successfully installed ruby-odbc-0.99994

The Fix

Within the gem folder I patched the odbc path to /usr/lib/odbc by

/usr/lib/ruby/gems/1.8/gems/ruby-odbc-0.99994/ext $ sudo ruby extconf.rb --with-odbc-dir=/usr/lib/odbc --disable-dlopen

Then install

/usr/lib/ruby/gems/1.8/gems/ruby-odbc-0.99994/ext $ sudo make install

Now

require "dbi"; dbh = DBI.connect('dbi:ODBC:MyLegacyServer', 'my_name', 'my_password')

=> #<DBI::DatabaseHandle:0x7f6ae61f22c0 @trace_mode=nil, @driver_name="odbc", @handle=#<DBI::DBD::ODBC::Database:0x7f6ae61f2158 @handle=#<ODBC::Database:0x7f6ae61f21a8>, @attr={}>, @convert_types=true, @trace_output=nil>

select = dbh.prepare('select * from my_table')
select.execute
while rec = select.fetch do
  puts rec.join(", ")
end

worked fine

Note

If this didn't help, perhaps another guide Show archive.org snapshot may help. It says

you need to install unixODBC before FreeTDS: The ./configure step of FreeTDS is dependent on unixODBC being installed: ./configure —with-unixodbc=/usr/local
Posted to makandra dev (2012-03-23 09:22)