DB0201EN-Week3-1-2-Querying-v4-py: 1.1 Task 1: Import The Ibm - DB Python Library
DB0201EN-Week3-1-2-Querying-v4-py: 1.1 Task 1: Import The Ibm - DB Python Library
1 Introduction
This notebook illustrates how to access your database instance using Python by following the
steps below: 1. Import the ibm_db Python library 1. Identify and enter the database connection
credentials 1. Create the database connection 1. Create a table 1. Insert data into the table 1.
Query data from the table 1. Retrieve the result set into a pandas dataframe 1. 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.
When the command above completes, the ibm_db library is loaded in your notebook.
1
dsn_driver = "{IBM DB2 ODBC DRIVER}"
dsn_database = "BLUDB" # e.g. "BLUDB"
dsn_hostname = "dashdb-txn-sbox-yp-dal09-08.services.dal.bluemix.net" ␣
,→ # e.g.: "dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net"
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() )
2
1.5 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
[5]: #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(25), lname VARCHAR(25), city VARCHAR(25), ccode CHAR(2))"
#Now fill in the name of the method and execute the statement
createStmt = ibm_db.exec_immediate(conn, createQuery)
,→'CA'),(2,'Raul','Chong','Markham','CA'),(3,'Hima','Vasudevan','Chicago','US')"
insertQuery2 = "..."
3
[11]: #Construct the query that retrieves all rows from the INSTRUCTOR table
selectQuery = "select * from INSTRUCTOR"
#Fetch the Dictionary (for the first row only) - replace ... with your code
ibm_db.fetch_both(selectStmt)
[11]: {'ID': 1,
0: 1,
'FNAME': 'Rav',
1: 'Rav',
'LNAME': 'Ahuja',
2: 'Ahuja',
'CITY': 'TORONTO',
3: 'TORONTO',
'CCODE': 'CA',
4: 'CA'}
4
[18]: #connection for pandas
pconn = ibm_db_dbi.Connection(conn)
#print just the LNAME for first row in the pandas data frame
pdf.LNAME[0]
[21]: 'Ahuja'
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
[23]: pdf.shape
[23]: (3, 5)
[24]: True
1.10 Summary
In this tutorial you established a connection to a database instance of DB2 Warehouse on Cloud
from a Python notebook using ibm_db API. Then created a table and insert a few rows of data
into it. Then queried the data. You also retrieved the data into a pandas dataframe.
Copyright © 2017-2018 cognitiveclass.ai. This notebook and its source code are released under the
terms of the MIT License.
[ ]: