COMPUTER SCIENCE INVESTIGATORY
PROJECT
NAME: Aayush Singh
CLASS: XII-A
BOARD ROLL NO.:
GUIDED BY: Ms. Shagufa Khan
SCHOOL: St. Xavier’s High School
             INDEX
SR.NO         TOPIC             PAGE NO
 01          Declaration           1
 02       Acknowledgement          2
 03         Introduction           3
 04      Working Procedure         4
 05     Functions and Modules     5-6
 06         Source Code           7-23
 07            Output            23-30
 08         Bibliography          31
                DECLARATION
I, Aayush Singh, solemnly declare that the investigatory
project titled "Hospital Management System" is
based on my own work carried out during the course of
my study under the guidance of Ms. Shagufa Khan. I
assert that the statements made in this project are based
on the results of my research, analysis, and
experimentation. I further certify that all the coding is
the result of my personal efforts.
I also acknowledge that any external assistance received
during the project is duly credited in the bibliography.
                                    Mr Aayush Singh
           ACKNOWLEDGEMENT
I would like to express a deep sense of gratitude to my project
guide, Ms. Shagufa Khan, for her unwavering support and
guidance throughout the course of this project. Her expertise,
patience, and encouragement were instrumental in the
successful completion of this endeavor. I am truly thankful for
the knowledge and skills I have gained under her mentorship.
I extend my heartfelt appreciation to our esteemed Principal,
Mrs. Salma Pathan, for her encouragement. Her commitment
to fostering a culture of curiosity within our school has played
a pivotal role in the completion of this project.
Finally, I extend my gratefulness to my parents for their
motivation and support. Their constant belief in my abilities
and emotional support were instrumental in my success.
                 INTRODUCTION
The Hospital Management System is a system designed to
streamline hospital operations by managing information
effectively. A modern hospital requires a management system
to handle various tasks, such as:
   Adding and updating patient records
   Managing doctor schedules
   Tracking appointments
   Billing and payment processing
   Maintaining an inventory of medicines and equipment
Manual handling of these operations can be overwhelming
and error-prone. This project introduces an automated, file-
handling-based program to simplify these tasks, ensure
efficiency, and reduce errors.
            WORKING PROCEDURE
The system allows users to perform the following operations:
  1. Add a patient record: Create a new record for a patient.
  2. Modify an existing patient record: Update details of an
    existing patient.
  3. Delete a patient record: Remove a patient's record from
    the system.
  4. Search for a patient: Locate a patient by their ID or
    name.
  5. List all patient records: Display all the patient records
    stored in the system.
  6. Manage doctor schedules: Organize and oversee
    doctors' availability and appointments.
  7. Maintain an inventory of medical supplies: Track and
    manage medicines and equipment.
  8. Exit the system: Log out or close the application.
            FUNCTIONS AND MODULES
MODULES:
     import csv: Used for exporting records to CSV files.
     import mysql.connector: Facilitates the connection between
      Python and MySQL for database operations.
FUNCTIONS:
 1.   connect(): Establishes a connection between Python and
      MySQL.
 2.   cursor(): A control structure for row-by-row processing of
      records.
 3.   execute(): Executes SQL queries and retrieves records.
 4.   fetchall(): Returns all rows from a result set as tuples.
 5.   fetchone(): Returns one row from the result set as a tuple.
 6.   commit(): Makes changes permanent in the database.
 7.   exportToCSV(): Exports data to a CSV file.
 8.   addPatient(): Adds a new patient record.
 9.   Add Doctor(): Adds a new doctor record
 10. Book Appointment():     Books an Appointment
 11. Export Patient data(): Exports the patient data
  12. listPatients():   Lists all patient records.
                         SOURCE CODE
import mysql.connector
from mysql.connector import Error
import csv
def display_heading():
  """
  Displays a dynamic heading for the Hospital Management
System.
  """
  print("=" * 50)
  print(" " * 10 + "Hospital Management System")
  print("=" * 50)
def setupDatabase():
  """
  Automatically creates the database and tables if they don't exist.
  """
  try:
    connection = mysql.connector.connect(
         host='localhost',
      user='root',
      password='1234'       )
    if connection.is_connected():
      cursor = connection.cursor()
      # Create database if it doesn't exist
      cursor.execute("CREATE DATABASE IF NOT EXISTS
hospital_db")
      print("Database 'hospital_db' is ready!")
      # Use the database
      cursor.execute("USE hospital_db")
       # Create patients, doctors, and appointments tables if they
don't exist
      cursor.execute("""
        CREATE TABLE IF NOT EXISTS patients (
            PatientID VARCHAR(50) PRIMARY KEY,
            PatientName VARCHAR(100),
            Age INT,
            Gender VARCHAR(20),
            Disease VARCHAR(100)
        )
      """)
      print("Table 'patients' is ready!")
      cursor.execute("""
        CREATE TABLE IF NOT EXISTS doctors (
             DoctorID VARCHAR(50) PRIMARY KEY,
             DoctorName VARCHAR(100),
             Specialization VARCHAR(100),
             Schedule VARCHAR(100)
        )
      """)
      print("Table 'doctors' is ready!")
      cursor.execute("""
        CREATE TABLE IF NOT EXISTS appointments (
             AppointmentID INT AUTO_INCREMENT PRIMARY KEY,
             PatientID VARCHAR(50),
             DoctorID VARCHAR(50),
             AppointmentTime DATETIME,
           FOREIGN KEY (PatientID) REFERENCES
patients(PatientID),
             FOREIGN KEY (DoctorID) REFERENCES doctors(DoctorID)
        )
      """)
         print("Table 'appointments' is ready!")
  except Error as e:
    print(f"Error during database setup: {e}")
  finally:
    if connection.is_connected():
         cursor.close()
         connection.close()
         print("Database setup complete.")
def exportToCSV(table):
  """
  Exports the records of the given table to a CSV file.
  """
  try:
    connection = mysql.connector.connect(
         host='localhost',
         database='hospital_db',
         user='root',
         password='1234'       )
    if connection.is_connected():
         cursor = connection.cursor()
      # Execute query to fetch all records from the table
      cursor.execute(f"SELECT * FROM {table}")
      records = cursor.fetchall()
      # Writing records to the CSV file
      with open(f'{table}.csv', mode='w', newline='') as file:
        writer = csv.writer(file)
        # Write header based on table
        if table == 'patients':
          writer.writerow(["PatientID", "PatientName", "Age",
"Gender", "Disease"])
        elif table == 'doctors':
           writer.writerow(["DoctorID", "DoctorName",
"Specialization", "Schedule"])
        elif table == 'appointments':
          writer.writerow(["AppointmentID", "PatientID",
"DoctorID", "AppointmentTime"])
        writer.writerows(records)
      print(f"Records exported to '{table}.csv' successfully!")
  except Error as e:
    print(f"Error during CSV export: {e}")
  finally:
    if connection.is_connected():
         cursor.close()
         connection.close()
def newPatient():
  """
  Adds a new patient record.
  """
  try:
    connection = mysql.connector.connect(
         host='localhost',
         database='hospital_db',
         user='root',
         password='1234'      )
    if connection.is_connected():
         cursor = connection.cursor()
         print("Add a new Patient Record:")
         print("==========================")
         # Input patient details
       patient_id = input("Enter Patient ID: ")
       patient_name = input("Enter Patient Name: ")
       age = int(input("Enter Age: "))
       gender = input("Enter Gender: ")
       disease = input("Enter Disease: ")
       # Insert record into the MySQL table
       query = """
        INSERT INTO patients (PatientID, PatientName, Age,
Gender, Disease)
         VALUES (%s, %s, %s, %s, %s)
       """
       values = (patient_id, patient_name, age, gender, disease)
       cursor.execute(query, values)
       connection.commit()
       print("Patient Record Saved")
       exportToCSV("patients") # Update CSV
  except Error as e:
    print(f"Error: {e}")
  finally:
    if connection.is_connected():
         cursor.close()
         connection.close()
def newDoctor():
  """
  Adds a new doctor record.
  """
  try:
    connection = mysql.connector.connect(
         host='localhost',
         database='hospital_db',
         user='root',
         password='1234'          )
    if connection.is_connected():
         cursor = connection.cursor()
         print("Add a new Doctor Record:")
         print("==========================")
         # Input doctor details
         doctor_id = input("Enter Doctor ID: ")
         doctor_name = input("Enter Doctor Name: ")
         specialization = input("Enter Specialization: ")
       schedule = input("Enter Schedule: ")
       # Insert record into the MySQL table
       query = """
         INSERT INTO doctors (DoctorID, DoctorName,
Specialization, Schedule)
         VALUES (%s, %s, %s, %s)
       """
       values = (doctor_id, doctor_name, specialization, schedule)
       cursor.execute(query, values)
       connection.commit()
       print("Doctor Record Saved")
       exportToCSV("doctors") # Update CSV
  except Error as e:
    print(f"Error: {e}")
  finally:
    if connection.is_connected():
       cursor.close()
       connection.close()
def newAppointment():
 """
 Adds a new appointment record.
 """
 try:
   connection = mysql.connector.connect(
        host='localhost',
        database='hospital_db',
        user='root',
        password='1234'
   )
   if connection.is_connected():
        cursor = connection.cursor()
        print("Book a new Appointment:")
        print("========================")
        # Input appointment details
        patient_id = input("Enter Patient ID: ")
        doctor_id = input("Enter Doctor ID: ")
    appointment_time = input("Enter Appointment Time (YYYY-
MM-DD HH:MM:SS): ")
        # Insert record into the MySQL table
        query = """
       INSERT INTO appointments (PatientID, DoctorID,
AppointmentTime)
          VALUES (%s, %s, %s)
        """
        values = (patient_id, doctor_id, appointment_time)
        cursor.execute(query, values)
        connection.commit()
        print("Appointment Booked")
        exportToCSV("appointments") # Update CSV
  except Error as e:
    print(f"Error: {e}")
  finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
def listPatients():
  """
  Lists all patient records.
  """
  try:
    connection = mysql.connector.connect(
         host='localhost',
         database='hospital_db',
         user='root',
         password='1234'
    )
    if connection.is_connected():
         cursor = connection.cursor()
         print("List of All Patient Records:")
         print("=============================")
         # Fetch all records
         query = "SELECT * FROM patients"
         cursor.execute(query)
         records = cursor.fetchall()
         if records:
           for record in records:
           print(f"PatientID: {record[0]}, Name: {record[1]}, Age:
{record[2]}, Gender: {record[3]}, Disease: {record[4]}")
         else:
         print("No patient records found!")
  except Error as e:
    print(f"Error: {e}")
  finally:
    if connection.is_connected():
       cursor.close()
       connection.close()
# Main Execution
if __name__ == "__main__":
  display_heading() # Display the heading dynamically
  setupDatabase()
  while True:
    print("\nMenu:")
    print("1. Add New Patient")
    print("2. Add New Doctor")
    print("3. Book an Appointment")
    print("4. List All Patients")
    print("5. Export Patient Data to CSV")
    print("6. Exit")
    choice = input("Enter your choice: ")
if choice == '1':
  newPatient()
elif choice == '2':
  newDoctor()
elif choice == '3':
  newAppointment()
elif choice == '4':
  listPatients()
elif choice == '5':
  exportToCSV("patients")
elif choice == '6':
  print("Exiting program...")
  break
else:
  print("Invalid choice. Please try again.")
          OUTPUT
1. To add a new Patient Record
2. To Add New Doctor
3. Book An Appointment
 4. To List All Patients
5. Export Patient Data to
          CSV
         6. Exit
     Result
1.Data saved in Mysql
  2. Table Patients
   3.Table Doctors
4.Table Appointments
              Bibliography
 1. Computer Science Class XI & XII by Sumita
                      Arora
2. Website: https://python.mykvs.in/projects.php