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/02. Query a DB2 Database using Python.ipynb
Views: 4598
Kernel: Python 3
cognitiveclass.ai logo

Query a DB2 Database using Python

Objectives

After completing this lab you will be able to:

  • Create a table

  • Insert data into the table

  • Query data from the table

  • Retrieve the result set into a pandas dataframe

  • Close the database connection

Notice: Please follow the instructions given in the first Lab of this course to Create a database service instance of Db2 on Cloud.

Task 1: 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 import the ibm_db library into our Python Application

!pip install ibm_db import ibm_db
Requirement already satisfied: ibm_db in c:\users\dabarnes\anaconda3\lib\site-packages (3.0.4)

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

Task 2: 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

  • 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

Replace the placeholder values in angular brackets <> below with your actual database credentials

e.g. replace "database" with "BLUDB"

#Replace the placeholder values with the actuals for your Db2 Service Credentials #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"

Task 3: Create the database connection

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

Create the database connection

#Create database connection #DO NOT MODIFY THIS CELL. Just RUN it with Shift + Enter 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) 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

Task 4: Create a table in the database

In this step we will create a table in the database with following details:

#Lets first drop the table INSTRUCTOR in case it exists from a previous attempt dropQuery = "drop table INSTRUCTOR" #Now execute the drop statment dropStmt = ibm_db.exec_immediate(conn, dropQuery)
--------------------------------------------------------------------------- Exception Traceback (most recent call last) <ipython-input-4-83413676a2ca> in <module> 3 4 #Now execute the drop statment ----> 5 dropStmt = ibm_db.exec_immediate(conn, dropQuery) SQLCODE=-204ion: [IBM][CLI Driver][DB2/LINUXX8664] SQL0204N "KFM42587.INSTRUCTOR" is an undefined name. SQLSTATE=42704

Dont worry if you get this error:

If you see an exception/error similar to the following, indicating that INSTRUCTOR is an undefined name, that's okay. It just implies that the INSTRUCTOR table does not exist in the table - which would be the case if you had not created it previously.

Exception: [IBM][CLI Driver][DB2/LINUXX8664] SQL0204N "ABC12345.INSTRUCTOR" is an undefined name. SQLSTATE=42704 SQLCODE=-204

#Construct the Create Table DDL statement - replace the ... with rest of the statement createQuery = "create table INSTRUCTOR(ID INTEGER PRIMARY KEY NOT NULL, FNAME VARCHAR(20), LNAME VARCHAR(20), CITY VARCHAR(20), CCODE CHAR(2))" #Now fill in the name of the method and execute the statement createStmt = ibm_db.exec_immediate(conn, createQuery)

Task 5: Insert data into the table

In this step we will insert some rows of data into the table.

The INSTRUCTOR table we created in the previous step contains 3 rows of data:

We will start by inserting just the first row of data, i.e. for instructor Rav Ahuja

#Construct the query - replace ... with the insert statement insertQuery = "insert into INSTRUCTOR values (1, 'Rav', 'Ahuja', 'TORONTO', 'CA')" #execute the insert statement insertStmt = ibm_db.exec_immediate(conn, insertQuery)

Now use a single query to insert the remaining two rows of data

#replace ... with the insert statement that inerts the remaining two rows of data insertQuery2 = "insert into INSTRUCTOR values (2, 'Raul', 'Chong', 'Markham', 'CA'),(3,'Hima', 'Vasudevan', 'Chicago', 'US')" #execute the statement insertStmt2 = ibm_db.exec_immediate(conn, insertQuery2)

Task 6: Query data in the table

In this step we will retrieve data we inserted into the INSTRUCTOR table.

#Construct the query that retrieves all rows from the INSTRUCTOR table selectQuery = "select * from INSTRUCTOR" #Execute the statement selectStmt = ibm_db.exec_immediate(conn, selectQuery) #Fetch the Dictionary (for the first row only) - replace ... with your code ibm_db.fetch_both(selectStmt)
{'ID': 1, 0: 1, 'FNAME': 'Rav', 1: 'Rav', 'LNAME': 'Ahuja', 2: 'Ahuja', 'CITY': 'TORONTO', 3: 'TORONTO', 'CCODE': 'CA', 4: 'CA'}
#Fetch the rest of the rows and print the ID and FNAME for those rows while ibm_db.fetch_row(selectStmt) != False: print (" ID:", ibm_db.result(selectStmt, 0), " FNAME:", ibm_db.result(selectStmt, "FNAME"))
ID: 2 FNAME: Raul ID: 3 FNAME: Hima

Bonus: now write and execute an update statement that changes the Rav's CITY to MOOSETOWN

#Enter your code below updateQuery = "update INSTRUCTOR set CITY='MOOSETOWN' where FNAME='Rav'" updateStmt = ibm_db.exec_immediate(conn, updateQuery)

Task 7: Retrieve data into Pandas

In this step we will retrieve the contents of the INSTRUCTOR table into a Pandas dataframe

import pandas import ibm_db_dbi
#connection for pandas pconn = ibm_db_dbi.Connection(conn)
#query statement to retrieve all rows in INSTRUCTOR table selectQuery = "select * from INSTRUCTOR" #retrieve the query results into a pandas dataframe pdf = pandas.read_sql(selectQuery, pconn) #print just the LNAME for first row in the pandas data frame pdf.LNAME[0]
'Ahuja'
#print the entire data frame pdf

Once the data is in a Pandas dataframe, you can do the typical pandas operations on it.

For example you can use the shape method to see how many rows and columns are in the dataframe

pdf.shape
(3, 5)

Task 8: Close the Connection

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