ODBC adapter connection help please

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

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 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.

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. [email protected]
    # Date: 10/14/2004
    #
    # Modifications: DeLynn B. [email protected]
    # Date: 3/22/2005

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

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. [email protected] 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


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

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.

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, [email protected] [email protected] 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. <[email protected]>
   #   Date: 3/22/2005

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


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


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

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

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.