SOURCE CODE
import mysql.connector
import pandas as pd
import matplotlib.pyplot as plt
def get_connection():
return mysql.connector.connect(
host="localhost",
user="root",
password="your_password", # change to your MySQL password
database="hospital_db"
def add_record():
con = get_connection()
cur = con.cursor()
appointment_id = int(input("Enter Appointment ID: "))
patient_name = input("Enter Patient Name: ")
patient_age = int(input("Enter Patient Age: "))
gender = input("Enter Gender (M/F): ")
doctor_name = input("Enter Doctor Name: ")
specialization = input("Enter Doctor Specialization: ")
appointment_date = input("Enter Appointment Date (YYYY-MM-DD): ")
cur.execute("""
INSERT INTO hospital
VALUES (%s, %s, %s, %s, %s, %s, %s)
""", (appointment_id, patient_name, patient_age, gender, doctor_name, specialization,
appointment_date))
con.commit()
con.close()
print("Record added successfully!")
def view_records():
con = get_connection()
df = pd.read_sql("SELECT * FROM hospital", con)
con.close()
print(df)
def report_patients_by_gender():
con = get_connection()
df = pd.read_sql("SELECT gender, COUNT(*) as total FROM hospital GROUP BY gender", con)
con.close()
print(df)
plt.bar(df["gender"], df["total"])
plt.title("Patients by Gender")
plt.xlabel("Gender")
plt.ylabel("Count")
plt.show()
def report_doctors_by_specialization():
con = get_connection()
df = pd.read_sql("SELECT specialization, COUNT(*) as total FROM hospital GROUP BY
specialization", con)
con.close()
print(df)
plt.pie(df["total"], labels=df["specialization"], autopct="%1.1f%%")
plt.title("Appointments by Doctor Specialization")
plt.show()
def main():
while True:
print("\n--- Hospital Management (One Table) ---")
print("1. Add Record")
print("2. View Records")
print("3. Report: Patients by Gender")
print("4. Report: Doctors by Specialization")
print("5. Exit")
choice = input("Enter choice: ")
if choice == "1":
add_record()
elif choice == "2":
view_records()
elif choice == "3":
report_patients_by_gender()
elif choice == "4":
report_doctors_by_specialization()
elif choice == "5":
print("Exiting...")
break
else:
print("Invalid choice! Try again.")
if _name_ == "_main_":
main()