0% found this document useful (0 votes)
114 views

Menu Driven Program Studentrecords

This Python code defines functions to add, update, delete, and display student records stored in a MySQL database. It uses MySQL connector to connect to a local database called 'school' and allows a user to select an option to perform CRUD operations by calling the corresponding function and prompting for required inputs.

Uploaded by

Eshaan Kuttikad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Menu Driven Program Studentrecords

This Python code defines functions to add, update, delete, and display student records stored in a MySQL database. It uses MySQL connector to connect to a local database called 'school' and allows a user to select an option to perform CRUD operations by calling the corresponding function and prompting for required inputs.

Uploaded by

Eshaan Kuttikad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import mysql.

connector as sql

def addrecord():
db = sql.connect(host='localhost',user = 'root', passwd = 'P@5sw0rd', database
= 'school')
cursor = db.cursor()
rollno = int(input('Enter rollno of student: '))
name = input('Enter name of student: ')
marks = int(input('Enter student marks: '))
grade = input("Enter student grade: ")
cursor.execute(f"insert into studentdata(ROLLNO,NAME,MARKS,GRADE)
values({rollno},'{name}',{marks},'{grade}')")
db.commit()
cursor.close()
db.close()

def updaterecord():
column = input('Enter column to be updated: ')
value = input('Enter new value: ')
rollno = input("Enter rollno to be updated: ")
db = sql.connect(host='localhost', user='root', passwd='P@5sw0rd',
database='school')
cursor = db.cursor()
try:
cursor.execute(f"UPDATE studentdata set {column} = {value} where ROLLNO =
{rollno}")
except:
cursor.execute(f"Update studentdata set {column} = '{value}' where ROLLNO =
{rollno}")
db.commit()
cursor.close()
db.close()

def deleterecord():
rollno = int(input("Enter rollno of student to be deleted"))
db = sql.connect(host='localhost', user='root', passwd='P@5sw0rd',
database='school')
cursor = db.cursor()
cursor.execute(f"delete from Studentdata where ROLLNO = {rollno}")
db.commit()
cursor.close()
db.close()

def displayrecord():
db = sql.connect(host='localhost', user='root', passwd='P@5sw0rd',
database='school')
cursor = db.cursor()
cursor.execute("Select * from Studentdata")
data = cursor.fetchall()
for i in data:
print(i)

while True:
print('''1 - add record
2 - update record
3 - delete record
4 - display record
5 - exit''')
ui = int(input('Enter option number: '))
if ui ==1:
addrecord()
elif ui ==2:
updaterecord()
elif ui ==3:
deleterecord()
elif ui ==4:
displayrecord()
elif ui == 5:
break
else:
print('Enter proper option number')

You might also like