Forum: Ruby on Rails ODBC adapter connection help please.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Larry K. (Guest)
on 2006-03-10 17:15
(Received via mailing list)
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 => "<dsn name>",
        :username => "<name>",
        :password => "<passwd>"
      )
    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
Derrick S. (Guest)
on 2006-03-10 17:59
(Received via mailing list)
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.
Chris H. (Guest)
on 2006-03-10 19:46
(Received via mailing list)
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.
unknown (Guest)
on 2006-03-10 20:26
(Received via mailing list)
>> 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>
       #   Date:   10/14/2004
       #
       #   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.
Larry K. (Guest)
on 2006-03-10 23:14
(Received via mailing list)
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> 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
>
>
>


--
Best Regards,
-Larry
"Work, work, work...there is no satisfactory alternative."
            --- E.Taft Benson
Larry K. (Guest)
on 2006-03-10 23:23
(Received via mailing list)
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> 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
>



--
Best Regards,
-Larry
"Work, work, work...there is no satisfactory alternative."
            --- E.Taft Benson
unknown (Guest)
on 2006-03-11 00:24
(Received via mailing list)
>>    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
Chris H. (Guest)
on 2006-03-11 04:53
(Received via mailing list)
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.
This topic is locked and can not be replied to.