You are on page 1of 65

Learning made simple

AppsTek Innovative Labs – Python


and DataBase Intergration 

www.appstekinnovativelabs.com | Distribution is Restricted. Copyright © 2021, AppsTek Inc.


Table of Contents
Why Database Programming?
Python Database Integration – Pre-requisites and Installation
SELECT Operation
 Retrieve Data from Database
 Attributes of Cursor object
 Bind variables
CREATE and INSERT Operation
 Creating a table
 Insert Operation
 Inserting Multiple Records
UPDATE Operation
DELETE Operation
Exception Handling

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Learning Objectives

On completion of this module, the learner should be able to:


• Understand the need for database connectivity from Python

• Install cx_Oracle for connecting to Oracle database from Python program

• Use Select operation to retrieve data from Oracle tables

• Implement Positional and Named bind variables for writing dynamic queries

• Perform Create, Insert, Update and Delete operations

• Handle database related exceptions in Python

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Why Database Programming?

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


A Scenario

Susie is trying to access Facebook to convey birthday wishes to her friend Julie. Mistakenly she enters wrong password.
Immediately she was intimated by Facebook that the password she has entered is not correct.
• Why do you think this happened?

• Where are the username and password for Facebook login stored?

• How does Facebook know that Susie entered a wrong password?

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


What actually happens?

• There is some database at the back end of Facebook application where the login credentials for Susie are stored.

• Susie interacts with the Facebook application through a browser which internally connects to a database. The application
validates the credentials entered by Susie with the actual credentials stored in the database.
• If they do not match, Facebook returns with an error message informing Susie that she entered a wrong password. If they
match, Susie is able to login to her Facebook account successfully.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Need for Database Programming

• Database Programming is about interaction between the Application (Facebook in the previous example) and a Database.

• The Application could be written in any of the programming languages, and could connect to any of the Databases.

• In this module, we will learn how to connect to Oracle Database from Python Application and execute various SQL queries.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Python Oracle Integration-
Pre-requisites and Installation

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Python Modules for Database Connectivity
• Different Python Modules are available for connecting to various databases. They are given in the table
below:

• In this module, we will be connecting to Oracle Database from Python, hence we will use cx_Oraclemodule.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Pre-requisites
Python Installation
Download and Install Python 3

Oracle Installation
Download Oracle 11g Express Edition:
https://www.oracle.com/database/technologies/xe-downloads.html

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


cx_Oracle Installation
• Download and install the relevant version from the following link:
https://pypi.org/project/cx-Oracle/

• You can test successful installation of cx_Oracle by executing following command

• No output confirms successful installation. Otherwise, error message will be displayed

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Connect to Oracle Database
• After successful installation of cx_Oracle, we need to establish a connection with Oracle database from Python program.

• connect() function helps to connect to Oracle database from Python using database name, database username , password
and database service name.
• connect() function returns the Connectionobject. Access to the database is made available through connection objects.

• Connectionclass handles the connection to a database instance. It encapsulates a database session.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Closing Database Connection
• Connected Database must be closed at the end of the program using close() function

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Cursor for Database Operations
• A cursor holds the rows returned by SQL statement. It helps us to traverse over the records returned.

• We need a Cursor object to do all database operations.

• cursor() function is used to create cursor.

• Cursor is required for following database operations:

• Create/Alter/Drop a table

• Insert/Update/Delete records from table

• Execute database functions and stored procedures (not in purview of this module)

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


SELECT Operation

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Retrieve Data from Database

Once the connection with database is established, we can use Select queries to retrieve the data from database tables.

Refer to this link for demonstration of Select Operation discussed in the video.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Consider a Table
• Consider a table "Supplier" in Oracle database. Structure and sample data for this table is given below.

• Table Structure:

• Sample Data:

Note: We would refer to this table for all further examples.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Select Operation
• execute() function of cursor object is used to execute a database query.

• All the records fetched by the query will be stored in cursor_object.


• Now, how can we retrieve them?
• We can use fetchall() function to get the records from the cursor object.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Select Operation
• In the previous example we have fetched the details of all the suppliers from Supplier table.
• Suppose, we want to fetch the details of a particular supplier (say supplierid = 'S1001').
• How do we implement this?

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Consider the following 'item' table: Predict the output of the below Python code:
Note: Connection string (in connect() function) in the code has to be
modified as per your Oracle credentials.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Attributes of Cursor Object
1. rowcount
• Contains the number of rows fetched by the cursor.
• It's a read only attribute.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Attributes of Cursor Object
2. description
• Returns information about the retrieved columns in the cursor.
• Returns NONE after execution of Insert/Update/Delete statements.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Consider the following 'customer' table:
Predict the output of the Python code given below:

Note: Using triple quotes (three consecutive single or double


quotes), the line of code can be broken into several lines which is
not possible with single or double quotes.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Bind Variables

• Till now we have been passing explicit values to Oracle queries.

• We have already seen an example to fetch the record from Supplier table for supplier id = 'S1001'

• Now, consider a scenario where we want to fetch the details of a particular supplier based on supplier id but, the value of
supplier id should be taken from the user, through Python application at execution time.

• In this case, we will not be able to pass static values to the query.

• Here, we can pass the values to query dynamically through bind variables which can be mapped to Python variables.

• Two ways of using bind variables:

• Positional: execute(query(:bindVariable1,:bindVariable2), (value1, value2)

• Named: execute(query(:bindVariable1, :bindVariable2), {bindVariable1 ', value1, ' bindVariable2, value2})Note: value1
and value2 can be Python Variable names as well.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Bind Variables - Positional

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Bind Variables - Named

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Consider the 'customer' table again:
Try to execute following code in IDE and suggest
the output:

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Consider a table "Employer" in Oracle database. Structure and sample data for this table is given below

Table Structure:

Check SQL Scrpit1


www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.
Assignment (contd..)

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
InfoTech Systems wants to retrieve certain information regarding their employers. Help them implement the following
business requirements:

1)Retrieve the name and email id of all 'IT' companies in 'Bangalore'.

2)Retrieve the name, mobile number and email id of all companies in a given city whose Renewal Status is 'Active'. Accept
'city' and ‘functionalarea’ as an input from user. Use positional bind variables.

3)Reverse the order of passing the parameter values in the above program and observe the output.

4)Implement the scenario in question# 2 using named bind variables.

5)Reverse the order of passing of the bind variables in the above program and observe the output. Are you still getting
the same result?

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


CREATE and INSERT Operation

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Creating a Table
• We have seen how to fetch the data from a table in Oracle database from Python program.
• Let us see how can we create a table in Oracle database using cx_Oracle.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Insert Operation
• Once a table is created, we can insert records into the table from a Python program.

Refer to this link for demonstration of Select Operation discussed in the video

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Commit Operation
commit()function of connection object permanently saves the changes to database

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Insert Operation
Let us see how to insert rows in 'supplier' table in Oracle database.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Insert a record using positional bind variables

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Insert a record using named bind variables

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assigment
Consider the 'item' table:
How many rows will be inserted after executing the following code?
Note: Connection string (in connect() function) in the code has to be
modified as per your Oracle credentials.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
InfoTech Systems is creating an online application for automating the task of job search between employer
and job seekers.

Create a table 'Users' from Python code. The column details are given below:

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment (contd...)
Insert the following data intoUserstable using cx_Oracle as per the specifications provided below:

• Insert first row using hard-coded values in INSERT query.

• Insert second row using positional bind variables.

• Insert third row using named bind variables.

• Accept the values for fourth row from user and insert using bind variables.

• Fetch and display all the records from users table.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Inserting Multiple Records
• executemany() function of cursor object is used to execute the same SQL query for different set of data:

• Consider a table 'Employee' in Oracle database with following specifications:


Employee (employeeid NUMBER, employeename VARCHAR2(25))
• Inserting multiple records using positional bind variables.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Inserting Multiple Records (contd...)

• Inserting multiple records using named bind variables:

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Bloom Technology wants to maintain their employee's vehicle details to make parking facility flexible to the
employees.

1.Create the following Vehicle table as a part of the application. Specifications are provided below:

2.Insert the following records using executemany() function of cursor. Use positional bind variables.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment (contd...)
3.Insert two more rows using named bind variables(use executemany() function)

4. Fetch and display all the records from Vehicle table.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


UPDATE Operation

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Update Operation

• In the previous section, we have learnt to create a table "Supplier" and insert records in this table using cx_Oracle.

• Suppose, there is a need to update the email id of a particular supplier.

• How do we do it from a Python program?

Refer to this link for demonstration of Select Operation discussed in the video

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Update a record using positional bind variables

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Update a record using named bind variables

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Consider the table 'product':

Predict the output of the below code:

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Refer to the table 'users' created earlier. The existing table data for “users” table is given below:

1.Modify the username and usertype of the user with userid = 4 with the following values:
• Username: lookingforjob@yahoo.com
• UserType: Jobseeker
Fetch and observe the values of 'username' and 'usertype' of the user with 'userid = 4' before and after'update' operation.
2.Change the password for userid = 1. Accept the new password as an input from user. Fetch and observethe value of
'password' of the user with 'userid = 1' before and after 'update' operation.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment

• Consider the 'Vehicle' table . Currently 'Vehicleid' is an integer field with values starting from 2001 onwards.
• Update the values of 'Vehicleid' to start from 1001 onwards as shown below.
- Hint – Use loops
• Update the Vehiclename to "Mahindra" for vehicle with vehicle id 1003.
• Fetch and display the values before and after the update operation.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


DELETE Operation

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Delete Operation
• Let us say we want to delete the record of a supplier.
• How do we do it from Python program?

Refer to this link for demonstration of Select Operation discussed in the video.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Delete a record using positional bind variables

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Delete a record using named bind variables

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Consider the Vehicle table and predict the output of the given Python code:

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Consider a scenario from a State Banking organization. The account table is created to store the account details of a
customer (Assume every customer can have only one account). Use cx_Oracle module to implement the following
requirements from Python code.(Do not execute the queries in database directly)

1.Create the table 'Account' as per below specifications:

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment (contd...)
2.Insert the following rows in the table:

3.Display the customer id and account balance of the customer with maximum account balance.

4.Fetch the account balance of the customer with customer id 102 and store it in a Python variable – 'acct_bal'.

5.Increment 'acct_bal' with 2000 and update the 'Balance' field of the table (for that particular customer) with the new
value.

6.Fetch and observe the updated account balance of the customer with customer id 102.7.Delete the 'Current' accounts
with zero balance.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Rollback Operation
• We used commit() function to commit the changes to database.

• rollback() function of connection class can be used, if we do not want to commit the changes made to database. It
reverses the effect of all the statements after the last commit.
• Please note that changes once committed can not be rolled back.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Exception Handling

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Exception Handling
• Consider the previous example of deleting a record for a specific supplier. We use the following code:

• Assume that while writing the above query we mistakenly mentioned incorrect column name 'supplier_id'. instead of
actual column name 'supplierid'. What will happen in this scenario?
• Try to execute this. You will get the following exception while executing the query:

cx_Oracle.DatabaseError: ORA-00904: "SUPPLIER_ID": invalid identifier


• This will abruptly terminate the program and the further statements will not get executed. In such scenarios, Exception
Handling can be used for smooth exit from the program.
• cx_Oracle module allows us to fetch the details of error (error code and error message) generated by Oracle. The details
are stored in DatabaseError class.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Exception Handling

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
• Consider the 'item' table

Choose the correct error message which will be displayed.

Note: Connection string (in connect() function) in the code has to


be modified as per your Oracle credentials.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


Assignment
Consider 'users' table. It has following data:

There is a requirement to delete the record of user with 'userid' 2.


• Try to mention incorrect column name(e.g. user_id) and observe the error.

• Use exception handling to handle the exception appropriately. Display the error code and message.

• Try to give incorrect username for connection string and observe the error code and message.

• Provide a wrong table name while writing the query and observe the error message.

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.


THANKS

Have a nice day!

www.appstekinnovativelabs.com |  Distribution is Restricted. Copyright © 2021, AppsTek  Inc.

You might also like