LAB CYCLE-5
Raghav Nimje
1. Write a menu driven program make changes to the table Product (given in the
lab cycle question sheet):
(i) Add records
(ii) Search records based on P_ID
(iii) Delete records based on the product name
(iv) Display all records
(v) Modify records using P_ID given by user
Code-
conn = SQL.connect(host = 'localhost',user = 'root',passwd =
"<your_pwd>",database = "Student")
cursor = conn.cursor()
def Add():
PID,name,Manu,Price = input("Enter the Product ID: "),input("Enter the product
name: "),input("Enter the manufacturer name: "),int(input("Enter the price: "))
cursor.execute("Insert into Product
values('{}','{}','{}',{})".format(PID,name,Manu,Price))
conn.commit()
def Search():
PID = input("Enter the Poduct ID of the product you want to search for: ")
cursor.execute("select * from Product where P_ID ='{}'".format(PID))
data = cursor.fetchall()
if len(data) != 0:
print(data)
else:
print("Record does not exist")
def Delete():
name = input("Enter the name of the product you want to delete from the
table: ")
cursor.execute("delete from Product where ProductName='{}'".format(name))
conn.commit()
print("data related to",name,"has been deleted")
def Display_records():
cursor.execute("select * from Product")
data = cursor.fetchall()
if len(data) == 0:
print("Table is empty")
else:
for i in data:
print(i)
def Modify():
PID = input("Enter the Product ID of the product you want to change the record
of: ")
n = int(input("1 : Change Product Name\n2 : Change Manufacturer Name\n3 :
Change Product Price\n"))
if n == 1:
Name = input("Enter the new name of the product: ")
cursor.execute("update Product set ProductName = '{}' where P_ID =
'{}'".format(Name,PID))
conn.commit()
elif n == 2:
MName = input("Enter the name of the new manufacturer: ")
cursor.execute("update Product set Manufacturer = '{}' where P_ID =
'{}'".format(MName,PID))
conn.commit()
elif n == 3:
Price = int(input("Enter the new price of the product: "))
cursor.execute("update Product set Price = {} where P_ID =
'{}'".format(Price,PID))
conn.commit()
while True:
n = int(input("1 : Add Record\n2 : Search\n3 : Delete\n4 : Display Records\n5 :
Modify Record\n6 : Exit\n"))
if n == 1:
Add()
elif n == 2:
Search()
elif n == 3:
Delete()
elif n == 4:
Display_records()
elif n == 5:
Modify()
elif n == 6:
break
else:
print("Invalid choice selected.")
Output-
i)
ii)
iii)
iv)
v)
2. Write a menu driven program for student database, class table (rollno, name,
marks, percentage) as fields
Code-
Have=menu’s
conn to add, delete,modify,search
SQL.connect(host and display
= "localhost",user records based
= "root",passwd = on rollno)
"<your
pwd>",database = "student")
cursor = conn.cursor()
def Add():
roll,name,marks,Perc = int(input("Enter the student Roll number:
")),input("Enter the student name: "),int(input("Enter the student marks:
")),int(input("Enter the student percentage: "))
cursor.execute("Insert into class
values({},'{}',{},{})".format(roll,name,marks,Perc))
conn.commit()
def Delete():
roll = int(input("Enter the roll number of the student record you want to delete
from the table: "))
cursor.execute("delete from class where Roll_no={}".format(roll))
conn.commit()
print("data related to roll number",roll,"has been deleted")
def Modify():
roll = int(input("Enter the Roll number of the student whose record you want
to change: "))
n = int(input("1 : Change Name\n2 : Change marks\n3 : Change
percentage\n"))
if n == 1:
Name = input("Enter the new name of the student: ")
cursor.execute("update class set Name = '{}' where Roll_no =
{}".format(Name,roll))
conn.commit()
elif n == 2:
marks = int(input("Enter the new marks: "))
cursor.execute("update class set Marks = {} where Roll_no =
{}".format(marks,roll))
conn.commit()
elif n == 3:
perc = int(input("Enter the new percentage of the student: "))
cursor.execute("update class set Percentage = {} where Roll_no =
{}".format(perc,roll))
conn.commit()
def Search():
roll = int(input("Enter the roll number of the student you want to search for: "))
cursor.execute("select * from class where Roll_no = {}".format(roll))
data = cursor.fetchall()
if len(data) != 0:
print(data)
else:
print("Record does not exist")
def Display_records():
cursor.execute("select * from class")
data = cursor.fetchall()
if len(data) == 0:
print("Table is empty")
else:
for i in data:
print(i)
while True:
n = int(input("1 : Add Record\n2 : Search\n3 : Delete\n4 : Display Records\n5 :
Modify Record\n6 : Exit\n"))
if n == 1:
Add()
elif n == 2:
Search()
elif n == 3:
Delete()
elif n == 4:
Display_records()
elif n == 5:
Modify()
elif n == 6:
break
else:
print("Invalid choice selected.")
Output-
i)
ii)
iii)
iv)
v)