Write a menu-driven program to insert, display, search, update and delete student records in MySQL
Expt. No.29
Page No.58
Menu-driven database program
Aim
To write a menu-driven Python program that inserts, displays, searches, updates and deletes records in a MySQL STUDENT table.
Concepts used
Database Connectivity ApplicationsInsert, Update and Delete from PythonPerforming Queries Using Cursor
Algorithm
- Connect to the database and create a cursor.
- Define one function for each operation.
- Show a menu repeatedly; call the function for the user's choice.
- Commit after every change; close the connection on Exit.
Program
# Menu-driven student database program import mysql.connector con = mysql.connector.connect(host="localhost", user="root", password="your_password", database="school")cur = con.cursor() def insert(): roll = int(input("Roll number: ")) name = input("Name : ") marks = int(input("Marks : ")) cur.execute("INSERT INTO student VALUES (%s, %s, %s)", (roll, name, marks)) con.commit() print("Record added") def display(): cur.execute("SELECT * FROM student") for row in cur.fetchall(): print(row) def search(): roll = int(input("Roll number to search: ")) cur.execute("SELECT * FROM student WHERE rollno = %s", (roll,)) row = cur.fetchone() if row == None: print("Not found") else: print(row) def update(): roll = int(input("Roll number to update: ")) marks = int(input("New marks : ")) cur.execute("UPDATE student SET marks = %s WHERE rollno = %s", (marks, roll)) con.commit() print(cur.rowcount, "record(s) updated") def delete(): roll = int(input("Roll number to delete: ")) cur.execute("DELETE FROM student WHERE rollno = %s", (roll,)) con.commit() print(cur.rowcount, "record(s) deleted") while True: print("1.Insert 2.Display 3.Search 4.Update 5.Delete 6.Exit") ch = input("Choice: ") if ch == "1": insert() elif ch == "2": display() elif ch == "3": search() elif ch == "4": update() elif ch == "5": delete() elif ch == "6": con.close() print("Connection closed") break else: print("Invalid choice")
A marked line continues the line above it. In your file, write it on the same line.
Change your_password to your own MySQL password before you run it.
Table used
Table: student
+--------+-------+-------+ | rollno | name | marks | +--------+-------+-------+ | 1 | Asha | 82 | | 2 | Rohan | 67 | +--------+-------+-------+
Output
$ python pysql_menu.py
1.Insert 2.Display 3.Search 4.Update 5.Delete 6.Exit
Choice: 1
Roll number: 3
Name : Kabir
Marks : 91
Record added
1.Insert 2.Display 3.Search 4.Update 5.Delete 6.Exit
Choice: 2
(1, 'Asha', 82)
(2, 'Rohan', 67)
(3, 'Kabir', 91)
1.Insert 2.Display 3.Search 4.Update 5.Delete 6.Exit
Choice: 3
Roll number to search: 2
(2, 'Rohan', 67)
1.Insert 2.Display 3.Search 4.Update 5.Delete 6.Exit
Choice: 4
Roll number to update: 2
New marks : 72
1 record(s) updated
1.Insert 2.Display 3.Search 4.Update 5.Delete 6.Exit
Choice: 5
Roll number to delete: 1
1 record(s) deleted
1.Insert 2.Display 3.Search 4.Update 5.Delete 6.Exit
Choice: 2
(2, 'Rohan', 72)
(3, 'Kabir', 91)
1.Insert 2.Display 3.Search 4.Update 5.Delete 6.Exit
Choice: 6
Connection closed
✓ Printed by running this program, not typed by hand.
Result
The menu added Kabir, displayed all three records, found roll number 2, updated Rohan's marks to 72, deleted roll number 1 and displayed the final two records before closing the connection.
Viva voce: what the examiner may ask
01Why is the connection opened once at the start and not in every function?
Connecting is slow; one connection and cursor are shared by all the functions.
02Why is row == None checked in search()?
fetchone() returns None when no row matches.
03What happens if you insert a roll number that already exists?
MySQL raises an error because rollno is the primary key and must be unique.
Variations for the lab test
- Add an option to display students above given marks.
- Handle the duplicate roll number error with try-except.
- Add an option to display the class average.
! Common mistake Closing the connection inside one function makes every later menu option fail with a "not connected" error.