This topic describes how to connect to IBM DB2 from Domino.
Domino recommends the ibm_DB2 package for interacting with DB2 databases from Python.
Credential setup
There are several environment variables you must set up to store secure information about your DB2 connection. Set the following as Domino environment variables on your user account:
-
db_username
-
db_password
Read Environment variables for secure credential storage to learn more about Domino environment variables.
Usage
Read the ibm_db for detailed information on how to use the package. The following is a simple example for connecting to DB2 with ibm_db where:
-
You have set up the previously noted environment variables with the ` db_username` and
db_password
-
You’ve replaced 'my.host.name' with the host name for your machine
import ibm_db
import ibm_db_dbi
import pandas as pd
hostname = 'my.host.name'
port = 50001
username = os.environ['db_username']
password = os.environ['db_password']
def query_db(sql):
ibm_db_conn = ibm_db.connect("DATABASE=IBMPROD;HOSTNAME={};PORT={};PROTOCOL=TCPIP;UID={};PWD={};".format(hostname, port, username, password), "", "")
conn = ibm_db_dbi.Connection(ibm_db_conn)
df = pd.read_sql_query(sql, conn)
ibm_db.close(ibm_db_conn)
return df
sql_cmd = """
SELECT
*
FROM
table
"""
df_cmd = query_db(sql_cmd)
df_cmd
Further reading: IBM database server in Python