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

Posted . Visible to the public.

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

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
Profile picture of Martin Straub
Martin Straub
Last edit
Keywords
SQL, Server, 2005
License
Source code in this card is licensed under the MIT License.
Posted by Martin Straub to makandra dev (2012-03-23 08:22)