import sqlite3
# Function to establish a database connection
def connect_to_database(database_name):
try:
conn = sqlite3.connect(database_name)
return conn
except sqlite3.Error as e:
print(f"Database connection error: {e}")
return None
# Function to insert a new student
def insert_student(conn, fullname, gender):
try:
cursor = conn.cursor()
cursor.execute("INSERT INTO Student (Fullname, Gender) VALUES (?, ?)", (fullname, gender))
conn.commit()
print("Student inserted successfully.")
except sqlite3.Error as e:
print(f"Insertion error: {e}")
# Function to update a student
def update_student(conn, student_id, fullname, gender):
try:
cursor = conn.cursor()
cursor.execute("UPDATE Student SET Fullname = ?, Gender = ? WHERE IDStudent = ?", (fullname, gender, student_id))
conn.commit()
print("Student updated successfully.")
except sqlite3.Error as e:
print(f"Update error: {e}")
# Function to delete a student
def delete_student(conn, student_id):
try:
cursor = conn.cursor()
cursor.execute("DELETE FROM Student WHERE IDStudent = ?", (student_id,))
conn.commit()
print("Student deleted successfully.")
except sqlite3.Error as e:
print(f"Deletion error: {e}")
# Main program
if __name__ == "__main__":
database_name = "student.db" # Change this to your desired database name
conn = connect_to_database(database_name)
if conn:
while True:
print("\nStudent Database Menu:")
print("1. Insert Student")
print("2. Update Student")
print("3. Delete Student")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == '1':
fullname = input("Enter Fullname: ")
gender = input("Enter Gender (Male or Female): ")
insert_student(conn, fullname, gender)
elif choice == '2':
student_id = input("Enter Student ID to update: ")
fullname = input("Enter New Fullname: ")
gender = input("Enter New Gender (Male or Female): ")
update_student(conn, student_id, fullname, gender)
elif choice == '3':
student_id = input("Enter Student ID to delete: ")
delete_student(conn, student_id)
elif choice == '4':
break
else:
print("Invalid choice. Please try again.")
conn.close()