Professional Documents
Culture Documents
How To Implement CRUD Operations in Python On MySQL
How To Implement CRUD Operations in Python On MySQL
This post demonstrates how to implement CRUD operations in Python language on a MySQL database.
Python supports most of the popular databases to work with and implement CRUD operations. Some of the popular databases
include:
MySQL
Oracle
PostgreSQL
Microsoft SQL Server 2000
Sybase
MySQL Database
If you want to work with CRUD operations in Python, first of all, download and install MySQL Database. If you already
have a database, skip this step.
STEP 1:
STEP 2:
Create Database
The following code snippet creates a new database. If you already have a database, you may skip this step. The database
is created on the local server. You may want to use your server name, user id, and password.
The cursor() method creates a cursor object that is used to execute a SQL query by using the execute method. Once the
database object is used, we need to close it.
Create a table
The following code snippet creates a new database table using CREATE TABLE SQL query.
import mysql.connector
mysqldb=mysql.connector.connect(host="localhost",user="root",password="",database="dbpython")#established connection be
mycursor=mysqldb.cursor()#cursor() method create a cursor object
try:
#Execute SQL Query to insert record
mycursor.execute("insert into student values(1,'Sarfaraj',80),(2,'Kumar',89),(3,'Sohan',90)")
mysqldb.commit() # Commit is used for your changes in the database
print('Record inserted successfully...')
except:
# rollback used for if any error
mysqldb.rollback()
mysqldb.close()#Connection Close
Display Record
The following code uses a SELECT * SQL query to select data from a database table. The resultset is stored using
cursor.fetchall() method.
import mysql.connector
mysqldb=mysql.connector.connect(host="localhost",user="root",password="",database="dbpython")#established connection be
mycursor=mysqldb.cursor()#cursor() method create a cursor object
try:
mycursor.execute("select * from student")#Execute SQL Query to select all record
result=mycursor.fetchall() #fetches all the rows in a result set
for i in result:
roll=i[0]
name=i[1]
marks=i[2]
print(roll,name,marks)
except:
print('Error:Unable to fetch data.')
mysqldb.close()#Connection Close
Update Record
The following code uses an UPDATE SQL query to update an existing record.
import mysql.connector
mysqldb=mysql.connector.connect(host="localhost",user="root",password="",database="dbpython")#established connection be
mycursor=mysqldb.cursor()#cursor() method create a cursor object
try:
mycursor.execute("UPDATE student SET name='Ramu', marks=100 WHERE roll=1")#Execute SQL Query to update record
mysqldb.commit() # Commit is used for your changes in the database
print('Record updated successfully...')
except:
# rollback used for if any error
mysqldb.rollback()
mysqldb.close()#Connection Close
Delete Record
The following code uses a DELETE SQL query to delete a record from the table.
import mysql.connector
mysqldb=mysql.connector.connect(host="localhost",user="root",password="",database="dbpython")#established connection be
mycursor=mysqldb.cursor()#cursor() method create a cursor object
try:
mycursor.execute("DELETE FROM student WHERE roll=3")#Execute SQL Query to detete a record
mysqldb.commit() # Commit is used for your changes in the database
print('Record deteted successfully...')
except:
# rollback used for if any error
mysqldb.rollback()
mysqldb.close()#Connection Close
Summary
In this post, I covered MySQL database operations (create database, create table, insert, display, update and delete)
with Python and MySQL Connector driver. Thank you for reading!