Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
import mysql.connector

# Create a database connection
conn = mysql.connector.connect(
    host="your_database_host",
    user="your_username",
    password="your_password",
    database="your_database_name"
)

cursor = conn.cursor()

def insert_student():
    fullname = input("Enter Fullname: ")
    gender = input("Enter Gender (Male or Female): ")

    # Insert a new student record
    sql = "INSERT INTO Student (Fullname, Gender) VALUES (%s, %s)"
    values = (fullname, gender)
    cursor.execute(sql, values)

    conn.commit()

def update_student():
    student_id = input("Enter Student ID to update: ")
    fullname = input("Enter New Fullname: ")
    gender = input("Enter New Gender (Male or Female): ")

    # Update a student record
    sql = "UPDATE Student SET Fullname = %s, Gender = %s WHERE IDStudent = %s"
    values = (fullname, gender, student_id)
    cursor.execute(sql, values)

    conn.commit()

def delete_student():
    student_id = input("Enter Student ID to delete: ")

    # Delete a student record
    sql = "DELETE FROM Student WHERE IDStudent = %s"
    values = (student_id,)
    cursor.execute(sql, values)

    conn.commit()

def main():
    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':
            insert_student()
        elif choice == '2':
            update_student()
        elif choice == '3':
            delete_student()
        elif choice == '4':
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()

# Close the cursor and the database connection
cursor.close()
conn.close()