Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to connect to a DB2 database.

29 views
Skip to first unread message

Victor Reyes

unread,
Oct 28, 2004, 11:28:26 AM10/28/04
to
Hello Ruby world.
The platform:

ruby 1.8.2 (2004-07-29) [i386-mswin32]
Windows/XP
DB2 8.1.4
DBname = dvddbase

The questions:

1 - How do I talk (connect) to a DB2 database using Ruby?
2 - Any sample code that can be shared?

Thank you

Victor


Michael Neumann

unread,
Oct 28, 2004, 11:42:49 AM10/28/04
to

You need Ruby/DBI and Ruby/DB2. See RAA (raa.ruby-lang.org).
http://ruby-dbi.rubyforge.org

A simple example:

require 'dbi'

DBI.connect('dbi:DB2:dvddbase', 'username', 'password') do |dbh|
p dbh.select_all('SELECT * FROM table')
end

Hope this helps.

Regards,

Michael


Victor Reyes

unread,
Oct 28, 2004, 12:22:59 PM10/28/04
to
I noticed that I already have dbi installed as part of the 1.8.2 installation.
However, when I try to use it to test it, I get the following errors:

C:\Program Files\IBM\SQLLIB\BIN>ruby db2c.rb
C:/Ruby/lib/ruby/site_ruby/1.8/dbi/dbi.rb:499:in `load_driver': is
not a class/module (TypeError)
from C:/Ruby/lib/ruby/site_ruby/1.8/dbi/dbi.rb:401:in `_get_full_driver'
from C:/Ruby/lib/ruby/site_ruby/1.8/dbi/dbi.rb:381:in `connect'
from db2c.rb:5

Any idea?

Thank you


Michael Neumann

unread,
Oct 28, 2004, 12:53:00 PM10/28/04
to

Yes, you have to install Ruby/DB2, too ;-)

Regards,

Michael


Victor Reyes

unread,
Oct 28, 2004, 2:37:16 PM10/28/04
to
Mike,
Thank you for your help so far.
The new problem now is installing Ruby/DB2.

Right from the beginning I have a problem. the "export" command does
not work under windows/XP. Second, even when I set the DB2DIR
environmental variable via the "set" command, it still does not work.

C:\$user\ruby\ruby-db2-0.4>export DB2DIR=C:/db2/SQLLIB/
'export' is not recognized as an internal or external command,
operable program or batch file.

C:\$user\ruby\ruby-db2-0.4>

or using the "set" command, which appears to work:

C:\$user\ruby\ruby-db2-0.4>set DB2DIR=C:/db2/SQLLIB/

C:\$user\ruby\ruby-db2-0.4>

C:\$user\ruby\ruby-db2-0.4>ruby setup.rb config
setup.rb:714: warning: don't put space before argument parentheses
entering config phase...
config done.

C:\$user\ruby\ruby-db2-0.4>

C:\$user\ruby\ruby-db2-0.4>ruby setup.rb setup
setup.rb:714: warning: don't put space before argument parentheses
entering setup phase...
C:/Ruby/bin/ruby extconf.rb
checking for SQLConnect() in db2cli.lib... no
ABORT: Could not locate DB2 libraries or headers!
Please set DB2DIR to your DB2 directory, e.g. /usr/IBMdb2/V7.1 (UNIX)
or C:/SQLLIB (Windows)
setup failed
'system C:/Ruby/bin/ruby extconf.rb' failed
try "ruby setup.rb --help" for usage

C:\$user\ruby\ruby-db2-0.4>

Question:

Is there a windows/xp binary that can be downloaded? or any idea about
this new road-block?

Thank you

Victor


"Peña, Botp"

unread,
Oct 28, 2004, 11:43:13 PM10/28/04
to
Victor Reyes [mailto:victor...@gmail.com] wrote:
> C:\$user\ruby\ruby-db2-0.4>set DB2DIR=C:/db2/SQLLIB/

haven't tried it but will it help if you remove the last "/" ?

some scripts balks on simple typos esp on windows..


Victor Reyes

unread,
Oct 30, 2004, 8:35:00 AM10/30/04
to
I tried it without the "/" with the same result.

C:\$user\ruby\ruby-db2-0.4>set DB2DIR=C:/db2/SQLLIB

C:\$user\ruby\ruby-db2-0.4> ruby setup.rb config
setup.rb:714: warning: don't put space before argument parentheses
entering config phase...
config done.

C:\$user\ruby\ruby-db2-0.4> ruby setup.rb setup
setup.rb:714: warning: don't put space before argument parentheses
entering setup phase...
C:/Ruby/bin/ruby extconf.rb
checking for SQLConnect() in db2cli.lib... no
ABORT: Could not locate DB2 libraries or headers!
Please set DB2DIR to your DB2 directory, e.g. /usr/IBMdb2/V7.1 (UNIX)
or C:/SQLLIB (Windows)
setup failed
'system C:/Ruby/bin/ruby extconf.rb' failed
try "ruby setup.rb --help" for usage

C:\$user\ruby\ruby-db2-0.4>

Thanks,

Victor

Michael Neumann

unread,
Oct 30, 2004, 8:50:43 AM10/30/04
to
On Sat, Oct 30, 2004 at 09:35:00PM +0900, Victor Reyes wrote:
> I tried it without the "/" with the same result.
>
> C:\$user\ruby\ruby-db2-0.4>set DB2DIR=C:/db2/SQLLIB
> C:\$user\ruby\ruby-db2-0.4> ruby setup.rb config
> setup.rb:714: warning: don't put space before argument parentheses
> entering config phase...
> config done.
>
> C:\$user\ruby\ruby-db2-0.4> ruby setup.rb setup
> setup.rb:714: warning: don't put space before argument parentheses
> entering setup phase...
> C:/Ruby/bin/ruby extconf.rb
> checking for SQLConnect() in db2cli.lib... no
> ABORT: Could not locate DB2 libraries or headers!
> Please set DB2DIR to your DB2 directory, e.g. /usr/IBMdb2/V7.1 (UNIX)
> or C:/SQLLIB (Windows)
> setup failed
> 'system C:/Ruby/bin/ruby extconf.rb' failed
> try "ruby setup.rb --help" for usage
>
> C:\$user\ruby\ruby-db2-0.4>

BTW, which version of Ruby do you use (and which DB2)?.

Please have a look at ruby-db2-0.4\ext\db2\extconf.rb

Replace it with:

require "mkmf"

DB2LIB = "db2cli"
DB2DIR = "C:/db2/SQLLIB"

dir_config( "db2", DB2DIR + "/include", DB2DIR + "/lib" )

if have_header("sqlcli.h") and have_library(DB2LIB, "SQLConnect")
create_makefile "db2cli"
else
raise "abort"
end

Then try running "ruby extconf.rb" alone. Look at C:/db2/SQLLIB/lib for
a db2cli.XXX file... play a bit with the paths (e.g. try
"C:\\db2\\SQLLIB\\lib" etc.)...

Regards,

Michael


0 new messages