- Home
- Javascript
- Building the Student Management Functions
Building the Student Management Functions
Contents
Building the Student Management Functions
This part covers the five core functions that let the user manage student records directly.
def add_student():
roll_no = int(input("Enter Roll Number: "))
name = input("Enter Name: ").strip()
class_name = input("Enter Class: ").strip()
age = int(input("Enter Age: "))
phone = input("Enter Phone Number: ").strip()
conn = connect_db()
cur = conn.cursor()
cur.execute(
"INSERT INTO students (roll_no, name, class_name, age, phone) VALUES (?, ?, ?, ?, ?)",
(roll_no, name, class_name, age, phone)
)
conn.commit()add_student() collects the details one by one using input(), then inserts them into the students table using an INSERT query. Notice the ? symbols in the query, these are placeholders that get safely filled in with the actual values from the tuple, instead of directly pasting user input into the query text.
def view_students():
cur.execute("SELECT * FROM students ORDER BY roll_no")
rows = cur.fetchall()
for r in rows:
print(f"{r[0]:<10}{r[1]:<20}{r[2]:<10}{r[3]:<6}{r[4]:<15}")view_students() fetches every row from the students table using SELECT *, sorted by roll number, and prints them in neatly aligned columns using f-string formatting.
def search_student():
cur.execute("SELECT * FROM students WHERE roll_no = ?", (roll_no,))
row = cur.fetchone()search_student() uses a WHERE clause to fetch just one specific student matching the entered roll number, instead of pulling the entire table.
def update_student():
cur.execute("SELECT * FROM students WHERE roll_no = ?", (roll_no,))
row = cur.fetchone()
name = input(f"Name [{row[1]}]: ").strip() or row[1]
...
cur.execute(
"UPDATE students SET name=?, class_name=?, age=?, phone=? WHERE roll_no=?",
(name, class_name, age, phone, roll_no)
)update_student() first fetches the existing record, then lets the user retype only the fields they want to change. If a field is left blank, the or row[1] part simply keeps the old value. The UPDATE query then overwrites the record for that specific roll number.
def delete_student():
confirm = input(f"Are you sure you want to delete '{row[1]}'? (y/n): ").lower()
if confirm == "y":
cur.execute("DELETE FROM students WHERE roll_no = ?", (roll_no,))
cur.execute("DELETE FROM attendance WHERE roll_no = ?", (roll_no,))
cur.execute("DELETE FROM grades WHERE roll_no = ?", (roll_no,))delete_student() asks for confirmation before deleting anything, and once confirmed, it removes the student not just from the students table, but also from attendance and grades, so no leftover data stays linked to a roll number that no longer exists.
Student Management System using Python (Beginner-Friendly Project Guide)
Abhishek Pati








