Skip to content

Instantly share code, notes, and snippets.

@trashvin
Created January 19, 2020 15:28
Show Gist options
  • Save trashvin/5718575e9c61d9957a4682a4ca45be02 to your computer and use it in GitHub Desktop.
Save trashvin/5718575e9c61d9957a4682a4ca45be02 to your computer and use it in GitHub Desktop.
Connect to IBM DB2 instance from Jupyter Notebook using Python
import ibm_db
dsn_hostname = "dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net" # e.g.: "dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net"
dsn_uid = "121a1we" # e.g. "abc12345"
dsn_pwd = "xfgfdg121212" # e.g. "7dBZ3wWt9XN6$o0J"
dsn_driver = "{IBM DB2 ODBC DRIVER}"
dsn_database = "BLUDB" # e.g. "BLUDB"
dsn_port = "50000" # e.g. "50000"
dsn_protocol = "TCPIP" # i.e. "TCPIP"
#Create the dsn connection string
dsn = (
"DRIVER={0};"
"DATABASE={1};"
"HOSTNAME={2};"
"PORT={3};"
"PROTOCOL={4};"
"UID={5};"
"PWD={6};").format(dsn_driver, dsn_database, dsn_hostname, dsn_port, dsn_protocol, dsn_uid, dsn_pwd)
#print the connection string to check correct values are specified
print(dsn)
try:
conn = ibm_db.connect(dsn, "", "")
print ("Connected to database: ", dsn_database, "as user: ", dsn_uid, "on host: ", dsn_hostname)
except:
print ("Unable to connect: ", ibm_db.conn_errormsg() )
server = ibm_db.server_info(conn)
print ("DBMS_NAME: ", server.DBMS_NAME)
print ("DBMS_VER: ", server.DBMS_VER)
print ("DB_NAME: ", server.DB_NAME)
#Retrieve Metadata for the Database Client / Driver
client = ibm_db.client_info(conn)
print ("DRIVER_NAME: ", client.DRIVER_NAME)
print ("DRIVER_VER: ", client.DRIVER_VER)
print ("DATA_SOURCE_NAME: ", client.DATA_SOURCE_NAME)
print ("DRIVER_ODBC_VER: ", client.DRIVER_ODBC_VER)
print ("ODBC_VER: ", client.ODBC_VER)
print ("ODBC_SQL_CONFORMANCE: ", client.ODBC_SQL_CONFORMANCE)
print ("APPL_CODEPAGE: ", client.APPL_CODEPAGE)# close
print ("CONN_CODEPAGE: ", client.CONN_CODEPAGE)
#close
ibm_db.close(conn)
@Shicheng-Guo
Copy link

Shicheng-Guo commented Jan 31, 2022

dsn_uid and dsn_pwd is IBM cloud username and passwd, correct? I mean IBMid. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment