PW Akshay

Generation

fix invalid codeSun, 11 Dec 2022

import mysql.connector # Connect to the database db = mysql.connector.connect( host="localhost", user="root", password="", database="akshay" ) # Create a cursor to interact with the database cursor = db.cursor() cursor.execute("DROP TABLE students") cursor.execute("DROP TABLE courses") # Create a table for storing students cursor.execute(""" CREATE TABLE students ( id INT PRIMARY KEY, name VARCHAR(255), grade INT ) """) # Create a table for storing courses cursor.execute(""" CREATE TABLE courses ( id INT PRIMARY KEY, name VARCHAR(255), teacher VARCHAR(255) ) """) # Create a table for storing grades cursor.execute(""" CREATE TABLE grades ( id INT PRIMARY KEY, student_id INT, course_id INT, grade INT, FOREIGN KEY (student_id) REFERENCES students(id), FOREIGN KEY (course_id) REFERENCES courses(id) ) """) # Function to add a new student to the database def add_student(): # Prompt the user to enter student information name = input("Enter the student's name: ") grade = input("Enter the student's grade: ") # Insert the student into the database cursor.execute(f"INSERT INTO students (name, grade) VALUES ('{name}', {grade})") # Function to update a student's information def update_student(): # Prompt the user to enter the student's ID student_id = input("Enter the student's ID: ") # Prompt the user to enter the updated information name = input("Enter the updated name: ") grade = input("Enter the updated grade: ") # Update the student's information in the database cursor.execute(f"UPDATE students SET name = '{name}', grade = {grade} WHERE id = {student_id}") # Function to delete a student from the database def delete_student(): # Prompt the user to enter the student's ID student_id = input("Enter the student's ID: ") # Delete the student from the database cursor.execute(f"DELETE FROM students WHERE id = {student_id}") # Function to add a new course to the database def add_course(): # Prompt the user to enter course information name = input("Enter the course name: ") teacher = input("Enter the course teacher: ") # Insert the course into the database cursor.execute(f"INSERT INTO courses (name, teacher) VALUES ('{name}', '{teacher}')") # Function to update a course's information def update_course(): # Prompt the user to enter the course's ID course_id = input("Enter the course's ID: ") # Prompt the user to enter the updated information name = input("Enter the updated name: ") teacher = input("Enter the updated teacher:") # Function to delete a course from the database def delete_course(): # Prompt the user to enter the course's ID course_id = input("Enter the course's ID: ") # Delete the course from the database cursor.execute(f"DELETE FROM courses WHERE id = {course_id}") # Function to add a new grade to the database def add_grade(): # Prompt the user to enter the student's ID student_id = input("Enter the student's ID: ") # Prompt the user to enter the course's ID course_id = input("Enter the course's ID: ") # Prompt the user to enter the grade grade = input("Enter the grade: ") # Insert the grade into the database cursor.execute(f"INSERT INTO grades (student_id, course_id, grade) VALUES ({student_id}, {course_id}, {grade})") # Function to update a grade def update_grade(): # Prompt the user to enter the grade's ID grade_id = input("Enter the grade's ID: ") # Prompt the user to enter the updated grade grade = input("Enter the updated grade: ") # Update the grade in the database cursor.execute(f"UPDATE grades SET grade = {grade} WHERE id = {grade_id}") # Function to delete a grade from the database def delete_grade(): # Prompt the user to enter the grade's ID grade_id = input("Enter the grade's ID: ") # Delete the grade from the database cursor.execute(f"DELETE FROM grades WHERE id = {grade_id}") # Function to display a student's information def display_student(): # Prompt the user to enter the student's ID student_id = input("Enter the student's ID: ") # Query the database to retrieve the student's information cursor.execute(f"SELECT * FROM students WHERE id = {student_id}") student = cursor.fetchone() # Print the student's information print(f"ID: {student[0]}, Name: {student[1]}, Grade: {student[2]}") # Function to display a course's information def display_course(): # Prompt the user to enter the course's ID course_id = input("Enter the course's ID: ") # Query the database to retrieve the course's information cursor.execute(f"SELECT * FROM courses WHERE id = {course_id}") course = cursor.fetchone() # Print the course's information print(f"ID: {course[0]}, Name: {course[1]}, Teacher: {course[2]}") # Function to display a grade def display_grade(): # Prompt the user to enter the grade's ID grade_id = input("Enter the grade's ID: ") # Query the database to retrieve the grade cursor.execute(f"SELECT * FROM grades WHERE id = {grade_id}") grade = cursor.fetchone() # Print the grade #print(f"ID: {grade # Function to display all students def display_all_students(): # Query the database to retrieve all students cursor.execute("SELECT * FROM students") students = cursor.fetchall() # Print the students for student in students: print(f"ID: {student[0]}, Name: {student[1]}, Grade: {student[2]}") # Function to display all courses def display_all_courses(): # Query the database to retrieve all courses cursor.execute("SELECT * FROM courses") courses = cursor.fetchall() # Print the courses for course in courses: print(f"ID: {course[0]}, Name: {course[1]}, Teacher: {course[2]}") # Function to display all grades def display_all_grades(): # Query the database to retrieve all grades cursor.execute("SELECT * FROM grades") grades = cursor.fetchall() # Print the grades for grade in grades: print(f"ID: {grade[0]}, Student ID: {grade[1]}, Course ID: {grade[2]}, Grade: {grade[3]}") # Main program loop while True: # Display menu print("1. Add student") print("2. Update student") print("3. Delete student") print("4. Add course") print("5. Update course") print("6. Delete course") print("7. Add grade") print("8. Update grade") print("9. Delete grade") print("10. Display student") print("11. Display course") print("12. Display grade") print("13. Display all students") print("14. Display all courses") print("15. Display all grades") print("16. Exit") choice = input("Enter your choice: ") # Add a new student if choice == "1": add_student() # Update a student's information elif choice == "2": update_student() # Delete a student elif choice == "3": delete_student() # Add a new course elif choice == "4": add_course()

Questions about programming?Chat with your personal AI assistant