DB2 with python: Difference between revisions

From JoBaPedia
Jump to navigation Jump to search
 
Line 11: Line 11:


  sudo pip install ibm_db
  sudo pip install ibm_db
for this to succeed you need to have a db2 client installed (e.g. as instance owner) or set IBM_DB_HOME=/path/to/dsdriver.
This path has include/sqlcli1.h and other needed files and is provided by a db2 odbc driver install, a db2 client install or a db2 server install.


= Test Connection =
= Test Connection =

Latest revision as of 11:21, 10 November 2020

Prerequisites

I only tested python3

  • Python
  • Python-dev?

Install

no client software required, just the python module ibm_db

sudo pip install ibm_db

for this to succeed you need to have a db2 client installed (e.g. as instance owner) or set IBM_DB_HOME=/path/to/dsdriver. This path has include/sqlcli1.h and other needed files and is provided by a db2 odbc driver install, a db2 client install or a db2 server install.

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}, ...]