Path: blob/master/SQL for Data Science/Ibm_db2_lab_sql.ipynb
3074 views
import the ibm_db Python library
Enter the database connection credentials
Create the database connection
Close the database connection
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-6ad896182c24> in <module>
----> 1 import ibm_db
2 import ibm_db_dbi
ModuleNotFoundError: No module named 'ibm_db'
Now enter your database credentials below and execute the cell with Shift + Enter
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.
Lets build the dsn connection string using the credentials you entered above
dsn = "DRIVER={{IBM DB2 ODBC DRIVER}};" + \
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.
Creating Tables and Queries
This Section illustrates how to access your database instance using Python by following the steps below:
Import the ibm_db Python library
Identify and enter the database connection credentials
Create the database connection
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
Create a table in the database
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