DB2 with python

From JoBaPedia
Revision as of 23:09, 7 December 2015 by Joachim (talk | contribs) (→‎Example)
Jump to navigation Jump to search

Prerequisites

  • Python
  • Python-dev?

Install

no client software required, just the python module ibm_db

sudo pip install ibm_db

Test Connection

ibm_db can specify connections by

  • referencing db2cli.ini database definitions
  • referencing db2dsdriver.cfg definitions
  • directly

Example

import ibm_db
# conn = ibm_db.connect('alias', 'user', 'pass')
conn_str = "DATABASE=mspr;HOSTNAME=job4;PORT=50002;PROTOCOL=TCPIP;UID=db2inst2;PWD=secret;"   
conn = ibm_db.connect(conn_str, , )                                                     

import ibm_db_dbi
conni = ibm_db_dbi.Connection(conn)       

conni.tables('SYSCAT', '%')
[{'TABLE_SCHEM': 'SYSCAT', 'REMARKS': None, 'TABLE_TYPE': 'VIEW', 'TABLE_NAME': 'ATTRIBUTES', 'TABLE_CAT': None}, ...]