# ODBC adapter connection help please

**URL:** <https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508>\
**Category:** Rails\
**Created:** [March 10, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508 "2006-03-10T16:15:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508/1 "2006-03-10T16:15:47Z")

</div>

Does anyone have any experience getting ODBC to work? I could really  
use  
some help here.

I’m having trouble accessing a db2 database via odbc using rails. It’s  
an  
external database, that I import data from. I have placed the  
connection  
code in my model :

unless connected?  
establish\_connection(  
:adapter =\> “odbc”,  
:dsn =\> “”,  
:username =\> “”,  
:password =\> “”  
)  
end

When I call this via the console, I get a " unknown adapter" error.  
I’ve  
checked and an odbc file is included.

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508/2 "2006-03-10T16:59:07Z")

</div>

Actually, I don’t think anyone has written a generic odbc adapter for  
rails. I could be wrong, but I don’t see one in trunk. There IS,  
however, a db2 adapter. You can use :adapter =\> ‘db2’ to connect to  
your database. According to [api.rubyonrails.com](http://api.rubyonrails.com) the db2 adapter  
takes the following options:

:username â?? Defaults to nothing  
:password â?? Defaults to nothing  
:database â?? The name of the database. No default, must be provided.  
Of course, I’ve never used db2 myself, so I’m sure someone else on  
the list can offer more insight.

-Derrick S.

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508/3 "2006-03-10T19:26:53Z")

</div>

> > Does anyone have any experience getting ODBC to work? I could really use  
> > some help here.
> > 
> > I’m having trouble accessing a db2 database via odbc using rails.  
> > There is currently no ODBC adapter for Rails.

For access to MSSQL (from rails on Linux), I did use:

- Installed and configured ODBC on the Linux box to connect to MS SQL
- Used an adapted ActiveRecord connection adapter:  
# odbc\_adapter.rb – ActiveRecord adapter for ODBC  
#  
# adapted from:  
# sqlserver\_adapter.rb – ActiveRecord adapter for Microsoft  
# SQL Server  
#  
# Author: Joey G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
# Date: 10/14/2004  
#  
# Modifications: DeLynn B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
# Date: 3/22/2005

It worked, but not perfectly; I definitely prefer to use a native  
adapter.

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508/4 "2006-03-10T22:14:37Z")

</div>

Thanks for the replies. everyone.

Chris, This looks close to what need. I’ll try it out this afternoon.  
Without looking at the docs, can I assume that the ‘LIB’ in the  
connection  
setup refers to the DSN that I will be using?

Thanks, again.  
-Larry

On 3/10/06, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I created a module in my lib dir and I require it where needed.  
> ‘something’")
> 
> ```
> flash.now ['error'] = "AS/400 Error: " + error
> 
> )
> --- E.Taft Benson
> 
> ```
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508/5 "2006-03-10T18:46:51Z")

</div>

There is currently no ODBC adapter for Rails. there is a db2 adapter,  
but i  
am not familiar with how weil it works.

I have a similar situation. I have to access data on an AS/400. only  
way  
is via odbc. I use ruby dbi and ruby odbc to retrieve the information  
then  
create AR objects from the returned data. Seems to work well for my  
situation since I am only retrieving data or calling stored procedures  
on  
the AS/400.

I created a module in my lib dir and I require it where needed.

for example:

lib/AS400.rb

module AS400  
def AS400.find\_something(something)  
begin  
dbh = DBI::connect(“DBI:ODBC:” + LIB, UID, PWD)  
row = dbh.select\_one(“select \* from table where something =  
‘something’”)  
rescue DBI::DatabaseError =\> e  
raise e.errstr  
ensure  
dbh.disconnect unless dbh.nil?  
end  
row.nil? ? nil : row[0]  
end  
end

then in the controllers where I need to use it, I just add

require\_dependency ‘AS400’

then in the action where you want to use it:

def some\_action  
begin  
@foo = Foo.new  
something = AS400.find\_something(@params[:something])  
@foo.name = something unless something.nil?  
rescue Exception =\> error  
flash.now[‘error’] = "AS/400 Error: " + error  
end  
end

hope this helps.

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508/6 "2006-03-10T22:23:41Z")

</div>

Did this work well enough for production?

When I have time, I intend to explore Rails on Linux. But, the Linux  
box  
would have to be able to communicate with the Windows LAN.

On 3/10/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Does anyone have any experience getting ODBC to work? I could really  
> > > use  
> > > some help here.
> > > 
> > > I’m having trouble accessing a db2 database via odbc using rails.  
> > > There is currently no ODBC adapter for Rails.

For access to MSSQL (from rails on Linux), I did use:

> ```
> #
> # Modifications: DeLynn B. <removed_email_address@domain.invalid>
> # Date: 3/22/2005
> 
> ```
> 
> It worked, but not perfectly; I definitely prefer to use a native adapter.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508/7 "2006-03-10T23:24:38Z")

</div>

> > For access to MSSQL (from rails on Linux), I did use:
> > 
> > - Installed and configured ODBC on the Linux box to connect to MS SQL
> > - Used an adapted ActiveRecord connection adapter:
> > 
> > It worked, but not perfectly; I definitely prefer to use a native adapter.

> Did this work well enough for production?

It worked well enough for my purposes at the time. But, it was an app  
for a  
very small user community (me + a few others), so I don’t have any idea  
how  
it would react to a large load. The two issues I had:  
\* Scaffold Generation  
\* Pagination Helper

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 11, 2006, 3:53am UTC](https://www.ruby-forum.com/t/odbc-adapter-connection-help-please/79508/8 "2006-03-11T03:53:31Z")

</div>

Larry,

yes, its the name of the datasource in odbc.ini

LIB/UID/PWD are just constants i set in config/environments/\* since i  
have  
different datasources for dev/prod/test.
