DB2 10.5 for Linux, UNIX, and Windows

Verifying the Python driver, SQLAlchemy adapter, and Django adapter installation

When the installation of the Python driver and optional adapters are complete, it is a good practice to test the new Python environment to verify that installation is working.

Before you begin

You must have the following software installed on your system:
  • Python 2.5 or later. For Linux operating systems, you also require the python2.5-dev package.
  • If your Python application connects to a remote IBM® database, the computer that runs your Python application requires one of the following products:
    • IBM Data Server Client
    • IBM Data Server Runtime Client
    • IBM Data Server Driver Package
    • IBM Data Server Driver for ODBC and CLI
  • If your Python application connects to local IBM database, no additional IBM Data Server products are required.
  • The Python environment must be configured for the listed driver and adapters:
    • ibm_db Python driver
    • ibm_db_sa SQLAlchemy adapter
    • ibm_db_django Django adapter

Procedure

To verify that your Python installation is successful:

  1. Using the python command, start the Python interpreter.
    $ python
  2. Using the listed code, test the ibm_db Python driver:
    import ibm_db
    ibm_db_conn = ibm_db.connect('database', 'user', 'password')
    import ibm_db_dbi
    conn = ibm_db_dbi.Connection(ibm_db_conn)
    conn.tables('SYSCAT', '%')
    You must specify a valid database name (database), user ID (user), and password (password) in the code. Successful connection indicates valid ibm_db Python driver installation.
  3. Optional: Using the listed code, test the ibm_db_sa SQLAlchemy adapter:
    import sqlalchemy
    from sqlalchemy import *
    import ibm_db_sa.ibm_db_sa
    db2 = sqlalchemy.create_engine('ibm_db_sa://user:password@host.name.com:50000/database')
    metadata = MetaData()
    users = Table('STAFF', metadata, 
    Column('ID', Integer, primary_key = True),
    Column('NAME', String(9), nullable = False),
    Column('DEPT', Integer, nullable = False),
    Column('JOB', String(5), nullable = False)
    )
    You must specify a valid database name (database), user ID (user), and password (password) in the sqlalchemy.create_engine argument string. Successful connection indicates valid ibm_db_django Django adapter installation.
  4. Optional: Using the listed code, test the ibm_db_django Django adapter:
    1. Using the django-admin.py startproject command, create a new Django project:
      django-admin.py startproject myproj
    2. Using the editor of your choice, edit DATABASES dictionary in the settings.py file to configure access to the IBM database server:
      DATABASES = {
        'default': {
        'ENGINE'   : 'ibm_db_django',
        'NAME'     : 'database',
        'USER'     : 'user',
        'PASSWORD' : 'password',
        'HOST'     : 'localhost',
        'PORT'     : '50000',
        'PCONNECT' :  True,      #Optional property. It is true by default
        }
      }
      You must specify a valid database name (database), user ID (user), password (password), host name (localhost), and port number (50000) in the settings.py file entry.
    3. Using the editor of your choice, add the following tuple of strings in the INSTALLED_APPS section of the settings.py file:
      'django.contrib.flatpages',
      'django.contrib.redirects',
      'django.contrib.comments',
      'django.contrib.admin',
    4. Using the manage.py application, verify the Django configuration:
      python manage.py test