DB2 10.5 for Linux, UNIX, and Windows

Testing the Python database connectivity with the db2dsdriver.cfg file

You can test the Python database connectivity with the db2dsdriver.cfg file to ensure that the db2dsdriver.cfg file is valid and the Python driver is installed correctly.

Before you begin

You must ensure that the IBM® data server client or IBM Data Server Driver Package software is installed and the db2dsdriver.cfg file is created in your environment with the required data source name (DSN) entry. You can use the contents of the db2dsdriver.cfg.sample file to create the db2dsdriver.cfg file.

You must have a working Python environment. For more information, see Python, SQLAlchemy and Django Framework application development for IBM data servers.

You must install the Python driver before testing the connection. Install the Python driver by using the easy_install tool. The easy_install is a Python module that installs and manages the Python packages. If you have a 32-bit Python and IBM Data Server driver environment, you must reference the 32-bit Python drivers packages (*.egg) that are in the <DB2_Install_Path>/python32 path.
$ easy_install <DB2_Install_Path>/python32/ibm_db-xx-xx-xx-xx.egg
$ easy_install <DB2_Install_Path>/python32/ibm_db_django-xx-xx.egg
$ easy_install <DB2_Install_Path>/python32/ibm_db_sa-xx-xx.egg
If you have a 64-bit Python and 64-bit IBM data server driver environment, you must reference the 64-bit Python drivers packages (*.egg) that are in the <DB2_Install_Path>/python64 path.
$ easy_install <DB2_Install_Path>/python64/ibm_db-xx-xx-xx.egg
$ easy_install <DB2_Install_Path>/python64/ibm_db_django-xx-xx.egg
$ easy_install <DB2_Install_Path>/python64/ibm_db_sa-xx-xx.egg
The <DB2_Install_Path> is the installation path of the IBM data server client or the IBM Data Server Driver Package software.The 32-bit Python environment must access the 32-bit CLI driver and the 32-bit Python driver. If you attempt to install the 64-bit Python driver by using the easy_install tool in a 32-bit Python environment or if the 64-bit CLI driver is installed, the following message is returned:
IBM DataServer environment not set

If you want to obtain and install the latest Python driver, see Setting up the Python environment for IBM data servers.

Procedure

To test the Python connectivity with the db2dsdriver.cfg file:

  1. Change path to the <DB2_Install_Path>/bin path.
  2. Issue the python validate_install.py <dsn_name> <username> <password> command. The <dsn_name> is the DSN in the db2dsdriver.cfg file that you are testing the connection to. The <username> is the valid user ID that is used to connect to the target data source. The <password> is the valid password that is associated with the specified user ID.
  3. Optional: You can also test the Python connection without the db2dsdriver.cfg file by using the python validate_install.py <database_name> <host_name> <port> <username> <password> command. The <database_name> is the database name that you are testing the connection to. The <host_name> is the host name of the server where the target database resides. The <port> is the database listener port that is configured on the database server. The <username> is the valid user ID that is used to connect to the target data source. The <password> is the valid password that is associated with the specified user ID.

Results

The python validate_install.py <dsn_name> command output displays the connection test status for the Python driver.
If you receive the following error message, you have a 32-bit Python environment but you configured the 64-bit IBM data server driver:
ImportError: wrong ELF class: ELFCLASS32
To configure the 32-bit IBM data server driver:
  1. Run or source the 32-bit profile script file. If you installed the 64-bit IBM Data Server Driver Package software:
    • In the 32-bit Bash or Korn shell environment, run the db2profile32 file.
    • In the 32-bit C shell environment, source the db2cshrc file.
  2. Use the easy_install tool to install the 32-bit Python driver.

Example

The following command output shows a successful connection to the sampledsn DSN entry:
$ python validate_install.py sampledsn username password
Connection to database sampledsn is successful.
$ python validate_install.py sample samplehost.domain.com 50000 username password
Connection to database sample is successful.