Translations of this page?:

IBM DB2

Using DB2 with Rails

Rails' support for IBM DB2 is provided directly from IBM itself through an open source gem called ibm_db. This gem contains a driver (written in C) that allows Ruby to communicate with DB2, and an adapter written in Ruby that enables ActiveRecord to work with DB2.

With the ibm_db gem installed, Rails can be used with any recent version 1) of the DB2 data server, including the unlimited data, free of charge edition known as DB2 Express-C, which is available for Linux, Unix, Windows, and more recently, Mac OS X. The ActiveRecord adapter (and Ruby driver) also support DB2 for System i and z/OS 2), and even Informix Dynamic Server (IDS) 11.10 and higher.

Generating a new application

To generate a Rails application with a config/database.yml file pre-populated for DB2, run the following command:

$ rails -d ibm_db my_app_name

database.yml example

Local (alias-based) connection:

development:
  adapter:     ibm_db
  username:    db2inst1
  password:    secret
  database:    db_name

Remote (TCP/IP-based) connection:

development:
  adapter:     ibm_db
  username:    db2inst1
  password:    secret
  database:    db_name
  host:        my.host.name
  port:        50000

The presence of host and port indicates to the adapter that a TCP/IP connection should be established. Database names are currently limited to 8 characters. The schema name defaults to the username, unless this has been set through a schema parameter. Other optional parameters available are account, app_user, application and workstation. The timeout parameter will be implemented in the next version of the ibm_db gem. Please note that at this time the pool parameter has not been implemented either (DB2 automatically provides connection pooling though).

Getting Help

If you require help, you are more than welcome to post your questions in the RubyForge forum for the project. Likewise, if your question is not related to Ruby/Rails and DB2, but more specific to DB2 itself, you can ask for help in the DB2 Express-C forum.

Both forums are frequented by IBMers and, in the case of RubyForge, by the authors of the ibm_db gem. 24/7 affordable commercial support for DB2 Express-C is also available.

Installation

Installing the ibm_db gem

The installation procedure is fairly straightforward as long as the following required steps are carefully executed.

Windows

On Windows, you can install the ibm_db gem by simply running:

$ gem install ibm_db

If prompted for a version from a list, select the latest mswin32 option available. When installing on Windows, the command retrieves a pre-built binary version of the driver, so you won't need any compilers or build tools.

Linux & Unix

On Linux and on Unix (e.g., IBM AIX) the gem is built from source; because of this, the installation process requires a few more steps.

Open up a shell as root by running:

$ sudo -s

Set the environment:

$ export IBM_DB_INCLUDE=/home/db2inst1/sqllib/include
$ export IBM_DB_LIB=/home/db2inst1/sqllib/lib

Source the DB2 profile 3):

$ . /home/db2inst1/sqllib/db2profile

Both pre-requisites above assume that the DB2 instance user is db2inst1, which is the default identifier.

Finally, install ibm_db by executing from the same shell:

$ gem install ibm_db

Once the gem is safely installed, you can exit from the root shell with exit.

Mac OS X

On Mac OS X Leopard (a Tiger version of DB2 doesn't exist), you can follow a set of steps that are virtually identical to those used for Linux:

$ sudo -s
$ export IBM_DB_INCLUDE=/Users/acangiano/sqllib/include
$ export IBM_DB_LIB=/Users/acangiano/sqllib/lib32
$ export ARCHFLAGS="-arch i386"
$ gem update --system
$ gem install ibm_db
$ exit

The only visible differences are the paths (which need to be adjusted for your own user), setting the architecture to let a 32 bit version of Ruby play nice with DB2 Express-C on Mac (which is 64 bit), and finally upgrading the version of RubyGems that comes with Leopard, if you haven't done so already.

1) Most people install both the client and server on their PC, but for the driver to work only the client is strictly required. In particular, older versions are supported from version 9 FixPack 2, and from version 8 FixPack 15.
2) For a DB2 client to connect from a PC to System i or z/OS, a valid DB2 Connect license is required.
3) This step is technically not required to build the driver, but it enables the user - other than the DB2 instance user (e.g., db2inst1) - to execute commands like db2 or db2level.

Discussion

hans dohler, 2009/04/30 17:54

hi all,

is there any change to get rails running with db2 V7.2. I know its a very old version, but we can't migrate at the moment since there are many dependencies and we want to try out rails just now.

I tried to install the adapter with gem install ibm_db – –with-IBM_DB-dir=/usr/IBMdb2/V7.1 –with-IBM_DB-include=/usr/IBMdb2/V7.1/include –with-IBM_DB-lib=/usr/IBMdb2/V7.1/lib

and get ibm_db.c ibm_db.c: In function `_ruby_ibm_db_connect_helper': ibm_db.c:1120: error: `SQL_ATTR_PING_DB' undeclared (first use in this function) ibm_db.c:1120: error: (Each undeclared identifier is reported only once ibm_db.c:1120: error: for each function it appears in.) ibm_db.c: At top level: ibm_db.c:4789: error: parse error before “SQLLEN” ibm_db.c: In function `_ruby_ibm_db_get_data2': ibm_db.c:4793: error: `ctype' undeclared (first use in this function) ibm_db.c:4796: error: `stmt_res' undeclared (first use in this function) ibm_db.c:4801: error: `col_num' undeclared (first use in this function) ibm_db.c:4802: error: `read_length' undeclared (first use in this function) ibm_db.c:4803: error: `buff' undeclared (first use in this function) ibm_db.c:4803: error: `buff_length' undeclared (first use in this function) ibm_db.c:4803: error: `out_length' undeclared (first use in this function) ibm_db.c: In function `ibm_db_server_info': ibm_db.c:5858: error: `SQL_DATABASE_CODEPAGE' undeclared (first use in this function) ibm_db.c: In function `ibm_db_client_info': ibm_db.c:6319: error: `SQL_APPLICATION_CODEPAGE' undeclared (first use in this function) ibm_db.c:6330: error: `SQL_CONNECT_CODEPAGE' undeclared (first use in this function) ibm_db.c: In function `ibm_db_active': ibm_db.c:6376: error: `SQL_ATTR_PING_DB' undeclared (first use in this function) make: *** [ibm_db.o] Fehler 1

Thanks a lot for any hint

Stefan

 
database-support/db2.txt · Last modified: 2009/02/27 15:40 by acangiano
 
Recent changes RSS feed Creative Commons License