Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DB2 example
"""
Import the ibm_db library
Will need to ensure that gcc is installed in the project environment as well. To do this will need to
go to the environment terminal and install gcc through yum using the following command.
sudo yum install gcc -y
After gcc installs you can install the pip package ibm_db using the following command.
pip install ibm_db
"""
import ibm_db
import json
# Get credentials from kubernetes. The credentials were setup as a dictionary
credentials = None
with open('/var/run/secrets/user_credentials/db2_credentials') as f:
credentials = json.load(f)
# Check and make sure the credentials were pulled correctly
if credentials:
username = credentials.get('username')
password = credentials.get('password')
hostname = credentials.get('hostname')
# Setup the connection to the database
connection = ibm_db.connect(
f"DATABASE=testing;HOSTNAME={hostname};PORT=50000;PROTOCOL=TCPIP;UID={username};PWD={password};",
"",
""
)
# Statement you want to execute
query = ibm_db.exec_immediate(connection, "select * from testing.employee")
# Loop through the results and print out the query
result = ibm_db.fetch_both(query)
while result:
print(result)
# Fetch the next item from the query
result = ibm_db.fetch_both(query)
ibm_db.close(connection)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment