CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
DanielBarnes18

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: DanielBarnes18/IBM-Data-Science-Professional-Certificate
Path: blob/main/06. Databases and SQL for Data Science with Python/04. Accessing Databases using Python/01. Connect to a DB2 Database using Python.ipynb
Views: 4598
Kernel: Python 3
cognitiveclass.ai logo

Connect to Db2 database on Cloud using Python

Objectives

After completing this lab you will be able to:

  • Import the ibm_db Python library

  • Enter the database connection credentials

  • Create the database connection

  • Close the database connection

Note: Please follow the instructions given in the first Lab of this course to Create a database service instance of Db2 on Cloud and retrieve your database Service Credentials.

Import the ibm_db Python library

The ibm_db API provides a variety of useful Python functions for accessing and manipulating data in an IBM® data server database, including functions for connecting to a database, preparing and issuing SQL statements, fetching rows from result sets, calling stored procedures, committing and rolling back transactions, handling errors, and retrieving metadata.

We first import the ibm_db library into our Python Application

Execute the following cell by clicking within it and then press Shift and Enter keys simultaneously

!pip install ibm_db #need to install first import ibm_db #then import
ERROR: Invalid requirement: '#need'

When the command above completes, the ibm_db library is loaded in your notebook.

Identify the database connection credentials

Connecting to dashDB or DB2 database requires the following information:

  • Driver Name

  • Database name

  • Host DNS name or IP address

  • Host port

  • Connection protocol

  • User ID (or username)

  • User Password

Notice: To obtain credentials please refer to the instructions given in the first Lab of this course

Now enter your database credentials below and execute the cell with Shift + Enter

#copied and pasted from service credentials of IBM Cloud Resource dsn_hostname = "dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net" dsn_uid = "kfm42587" dsn_pwd = "6zkhf3chpx0-m9cl" dsn_driver = "{IBM DB2 ODBC DRIVER}" dsn_database = "BLUDB" dsn_port = "50000" dsn_protocol = "TCPIP"

Create the DB2 database connection

Ibm_db API uses the IBM Data Server Driver for ODBC and CLI APIs to connect to IBM DB2 and Informix.

Build the dsn connection string using the credentials entered above

#DO NOT MODIFY THIS CELL. Just RUN it with Shift + Enter #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)
DRIVER={IBM DB2 ODBC DRIVER};DATABASE=BLUDB;HOSTNAME=dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net;PORT=50000;PROTOCOL=TCPIP;UID=kfm42587;PWD=6zkhf3chpx0-m9cl;

Now establish the connection to the database

#DO NOT MODIFY THIS CELL. Just RUN it with Shift + Enter #Create database connection 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() )
Connected to database: BLUDB as user: kfm42587 on host: dashdb-txn-sbox-yp-lon02-13.services.eu-gb.bluemix.net
#Retrieve Metadata for the Database Server server = ibm_db.server_info(conn) print ("DBMS_NAME: ", server.DBMS_NAME) print ("DBMS_VER: ", server.DBMS_VER) print ("DB_NAME: ", server.DB_NAME)
DBMS_NAME: DB2/LINUXX8664 DBMS_VER: 11.01.0404 DB_NAME: BLUDB
#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) print ("CONN_CODEPAGE: ", client.CONN_CODEPAGE)
DRIVER_NAME: DB2CLI.DLL DRIVER_VER: 11.05.0500 DATA_SOURCE_NAME: BLUDB DRIVER_ODBC_VER: 03.51 ODBC_VER: 03.01.0000 ODBC_SQL_CONFORMANCE: EXTENDED APPL_CODEPAGE: 1252 CONN_CODEPAGE: 1208

Close the Connection

We free all resources by closing the connection. Remember that it is always important to close connections so that we can avoid unused connections taking up resources.

ibm_db.close(conn)
True