70 Python Experiment No. 14 Nakhwa Arman Anis
70 Python Experiment No. 14 Nakhwa Arman Anis
70 Python Experiment No. 14 Nakhwa Arman Anis
3. Hardware Requirements:
1. PC with minimum 2GB RAM
4. Software Requirements:
1. Windows / Linux OS.
2. Python 3.6 or higher
5. Learning Objectives:
1. To understand database.
2. To understand how to install and start MySQL database server.
3. To know working with XAMPP GUI.
4. To understand how to perform CRUDE operations on MySQL database
What is SQL?
What is MySQLdb?
MySQLdb is an interface for connecting to a MySQL database server from Python. It
implements the Python Database API v2.0 and is built on top of the MySQL C API.
import MySQLdb
ImportError: No module named MySQLdb
To install MySQLdb module, for Python command prompt, use the following command -
pip install MySQL-python
Database Connection
Before connecting to a MySQL database, make sure of the followings −
You have created a database TESTDB.
You have created a table EMPLOYEE in TESTDB.
This table has fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.
User ID "testuser" and password "test123" are set to access TESTDB.
Python module MySQLdb is installed properly on your machine.
You have gone through MySQL tutorial to understand MySQL Basics.
Example
Following is the example of connecting with MySQL database "TESTDB"
import MySQLdb
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
Example
Let us create Database table EMPLOYEE −
import MySQLdb
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
AGE INT,
INSERT Operation
It is required when you want to create your records into a database table.
Example
The following example, executes SQL INSERT statement to create a record into EMPLOYEE
table –
#!/usr/bin/python import
MySQLdb
try:
except:
Example
Following code segment is another form of execution where you can pass parameters directly –
user_id = "test123"
password = "password"
Update Operation
UPDATE Operation on any database means to update one or more records, which are already
available in the database.
DELETE Operation
DELETE operation is required when you want to delete some records from your database
Performing Transactions
Transactions are a mechanism that ensures data consistency. Transactions have the following
four properties −
Atomicity − Either a transaction completes or nothing happens at all.
Consistency − A transaction must start in a consistent state and leave the system in a
consistent state.
Isolation − Intermediate results of a transaction are not visible outside the current
transaction.
Durability − Once a transaction was committed, the effects are persistent, even after a
system failure.
COMMIT Operation
Commit is the operation, which gives a green signal to database to finalize the changes, and after
this operation, no change can be reverted back.
ROLLBACK Operation
If you are not satisfied with one or more of the changes and you want to revert back those
changes completely, then use rollback() method.
Disconnecting Database
To disconnect Database connection, use close() method.
10. Results:
1. To Create Database
FAMT/ IT / Semester – IV (Rev-2019 ‘C’) / Lab experiment / Academic Year: 2020-21 / First Half 2021
2. Insert Data in database
FAMT/ IT / Semester – IV (Rev-2019 ‘C’) / Lab experiment / Academic Year: 2020-21 / First Half 2021
3. Delete record from database
FAMT/ IT / Semester – IV (Rev-2019 ‘C’) / Lab experiment / Academic Year: 2020-21 / First Half 2021
4. Update record in database
FAMT/ IT / Semester – IV (Rev-2019 ‘C’) / Lab experiment / Academic Year: 2020-21 / First Half 2021
5. To display the data stored in database on the Python Shell
FAMT/ IT / Semester – IV (Rev-2019 ‘C’) / Lab experiment / Academic Year: 2020-21 / First Half 2021
6.
FAMT/ IT / Semester – IV (Rev-2019 ‘C’) / Lab experiment / Academic Year: 2020-21 / First Half 2021
11. Learning Outcomes Achieved:
1. Got knowledge about database.
2. Understood how to install and start MySQL database server.
3. Got to know about working with XAMPP GUI.
4. Understood how to perform CRUDE operations on MySQL database
12. Conclusion:
Hence, we understood database programming in Python.
Experiment/Assignment Evaluation:
1 Technical Understanding (Assessment may be done based on Q & A or any other relevant 6
method.) Teacher should mention the other method used -
2 Neatness/presentation 2
3 Punctuality 2
References:
[1] James Payne, “Beginning Python using Python 2.6 and Python 3.1”, Wrox Publications.
[2] Dr. R. Nageswara Rao, “Core Python Programming”, Dreamtech Press, Wiley Publications.
[3] Charles R. Severance “Python for Everybody: Exploring Data in Python 3”
Viva Questions
1. What do you mean by database?
2. Why to prefer a standard database over file based data storage?
3. Which Python package is required to facilitate MySQL database operations in Python.
FAMT/ IT / Semester – IV (Rev-2019 ‘C’) / Lab experiment / Academic Year: 2020-21 / First Half 2021