Created
December 13, 2018 19:09
-
-
Save oldarmyc/bf99b2832fb20d2eb5d9e199e9f25bce to your computer and use it in GitHub Desktop.
DB2 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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