Python DB Integration
Python DB Integration
• Implement Positional and Named bind variables for writing dynamic queries
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?
• 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.
• 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.
• In this module, we will be connecting to Oracle Database from Python, hence we will use cx_Oraclemodule.
Oracle Installation
Download Oracle 11g Express Edition:
https://www.oracle.com/database/technologies/xe-downloads.html
• 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.
• Create/Alter/Drop a table
• Execute database functions and stored procedures (not in purview of this module)
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.
• Table Structure:
• Sample Data:
• 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.
• Named: execute(query(:bindVariable1, :bindVariable2), {bindVariable1 ', value1, ' bindVariable2, value2})Note: value1
and value2 can be Python Variable names as well.
Table Structure:
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.
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?
Refer to this link for demonstration of Select Operation discussed in the video
Create a table 'Users' from Python code. The column details are given below:
• Accept the values for fourth row from user and insert using bind variables.
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.
• In the previous section, we have learnt to create a table "Supplier" and insert records in this table using cx_Oracle.
Refer to this link for demonstration of Select Operation discussed in the video
1.Modify the username and usertype of the user with userid = 4 with the following values:
• Username: [email protected]
• 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.
• 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.
Refer to this link for demonstration of Select Operation discussed in the video.
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.
• 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.
• 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:
• 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.