🏥 HOSPITAL MANAGEMENT SYSTEM
Class XII – CBSE Computer Science Project
Objective
The objective of this project is to computerize the basic operations of a hospital such as patient registration, treatment details, billing, and discharge.
The system stores patient data securely in a MySQL database and generates bills automatically during discharge.
Tools & Technologies Used
- Python 3
- MySQL
- mysql-connector-python
- tabulate module
Scope
This project is suitable for small hospitals or clinics and strictly follows CBSE Class XII syllabus concepts such as:
- Functions
- Loops
- Conditional statements
- MySQL connectivity
- Menu-driven programs
📌 2. ACKNOWLEDGEMENT
I express my sincere gratitude to my Computer Science teacher for guiding me throughout this project.
I also thank my school for providing the necessary facilities to complete this project successfully.
Lastly, I thank my parents and friends for their continuous support.
📌 3. CERTIFICATE
This is to certify that xyz of Class XII has successfully completed the Computer Science project titled “Hospital Management System” as prescribed by CBSE for the academic year 2025–26.
The project is original and has been completed under my supervision.
Teacher’s Signature: _________
Date: _________
📌 4. FLOWCHART
START
|
Display Menu
|
User Choice?
|
Add / Modify / Search / Delete / Show / Discharge
|
Perform Operation
|
Return to Menu
|
EXIT
📌 6. COMPLETE PYTHON PROGRAM
📄 File Name: hospital_management.py
import mysql.connector as sqlcon
from tabulate import tabulate
from datetime import date
# -------------------------------
# Step 1: Connect to MySQL Server
# -------------------------------
con = sqlcon.connect(
host="localhost",
user="root"
#password="root" # change if required
)
cur = con.cursor()
# -------------------------------
# Step 2: Create Database if not exists
# -------------------------------
cur.execute("CREATE DATABASE IF NOT EXISTS Hospital")
cur.execute("USE Hospital")
# -------------------------------
# Step 3: Create Tables if not exists
# -------------------------------
cur.execute("""
CREATE TABLE IF NOT EXISTS patient (
patient_id INT AUTO_INCREMENT PRIMARY KEY,
patient_name VARCHAR(50),
admit_date DATE,
age INT,
gender VARCHAR(10),
contact_no VARCHAR(15),
disease VARCHAR(50),
room_type VARCHAR(10),
room_no INT
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS treatment (
treatment_id INT PRIMARY KEY,
patient_id INT,
treatment_name VARCHAR(50),
cost INT,
FOREIGN KEY (patient_id) REFERENCES patient(patient_id)
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS discharge (
patient_id INT PRIMARY KEY,
patient_name VARCHAR(50),
discharge_date DATE,
no_of_days INT,
treatment_cost INT,
room_charges INT,
grand_total INT
)
""")
print("✅ Database and tables checked/created successfully")
# -------------------------------------------------
# Step 4: Insert data only if patient table is empty
# -------------------------------------------------
cur.execute("SELECT COUNT(*) FROM patient")
count = cur.fetchone()[0]
if count == 0:
insert_query = """
INSERT INTO patient
(patient_name, admit_date, age, gender, contact_no, disease, room_type, room_no)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s)
"""
records = [
('Amit Kumar','2025-01-05',35,'Male','9876543210','Fever','General',101),
('Riya Sharma','2025-01-10',28,'Female','9876501234','Infection','Private',201),
('Rahul Verma','2025-01-12',45,'Male','9898989898','Heart','ICU',301),
('Neha Singh','2025-01-15',30,'Female','9123456780','Migraine','General',102),
('Suresh Mehta','2025-01-18',55,'Male','9000000001','Diabetes','Private',202)
]
cur.executemany(insert_query, records)
con.commit()
print("✅ Patient records inserted successfully")
# ---------------- FUNCTIONS ----------------
def add_patient():
name = input("Patient Name: ")
ad_date = input("Admit Date (YYYY-MM-DD): ")
age = int(input("Age: "))
gender = input("Gender: ")
contact = input("Contact No: ")
disease = input("Disease: ")
room_type = input("Room Type (General/Private/ICU): ")
room_no = int(input("Room No: "))
q = "INSERT INTO patient (patient_name,admit_date,age,gender,contact_no,disease,room_type,room_no) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
cur.execute(q,(name,ad_date,age,gender,contact,disease,room_type,room_no))
con.commit()
print("Patient Added Successfully")
def add_treatment():
tid = int(input("Treatment ID: "))
pid = int(input("Patient ID: "))
tname = input("Treatment Name: ")
cost = int(input("Cost: "))
q = "INSERT INTO treatment VALUES (%s,%s,%s,%s)"
cur.execute(q,(tid,pid,tname,cost))
con.commit()
print("Treatment Added")
def modify_patient():
pid = int(input("Patient ID: "))
name = input("New Name: ")
room = input("New Room Type: ")
q = "UPDATE patient SET patient_name=%s, room_type=%s WHERE patient_id=%s"
cur.execute(q,(name,room,pid))
con.commit()
print("Record Updated")
def search_patient():
pid = int(input("Patient ID: "))
cur.execute("SELECT * FROM patient WHERE patient_id=%s",(pid,))
data = cur.fetchall()
if data:
head=["Pid","Name","Ad_date","Age","Sex","Ph","Disease","Room","Room_no"]
print(tabulate(data,headers=head,tablefmt="grid"))
else:
print("-------------------------------")
print("|........Record not found......|")
print("-------------------------------")
def delete_patient():
pid = int(input("Patient ID: "))
cur.execute("SELECT * FROM treatment WHERE patient_id=%s",(pid,))
if cur.fetchone():
print("Cannot delete! Treatment record exists.")
else:
cur.execute("DELETE FROM patient WHERE patient_id=%s",(pid,))
con.commit()
print("Patient Deleted")
def show_records():
tables={'1':"patient",'2':"treatment",'3':"discharge"}
print("1: patient \n2: treatment \n3:discharge")
t = input("Select table (1/2/3): ")
if t=='1':
headers=["Pid","Name","Adate","Age","Sex","Ph","Disease","Rm_type","Rno"]
sql="SELECT * FROM patient"
out_format="outline"
elif t=='2':
headers=["Tid","Pid","Name","Treatment","Cost"]
sql="SELECT treatment_id,p.patient_id,patient_name,treatment_name,cost FROM treatment t, patient p where t.patient_id=p.patient_id"
out_format="grid"
elif t=='3':
headers=["Pid","Name","Ddate","Days","Cost","RCharg","Total"]
sql="SELECT * FROM discharge"
out_format="grid"
cur.execute(sql)
data = cur.fetchall()
print(tabulate(data,headers=headers,tablefmt=out_format))
def discharge_patient():
pid = int(input("Patient ID: "))
cur.execute("SELECT patient_name,admit_date,room_type FROM patient WHERE patient_id=%s",(pid,))
rec = cur.fetchone()
if not rec:
print("Invalid Patient ID")
return
name, ad_date, room = rec
d_date = date.today()
days = (d_date - ad_date).days
cur.execute("SELECT SUM(cost) FROM treatment WHERE patient_id=%s",(pid,))
t_cost = cur.fetchone()[0] or 0
if room=="General":
r_charge = days * 1500
elif room=="Private":
r_charge = days * 3000
else:
r_charge = days * 5000
total = t_cost + r_charge
q = "INSERT INTO discharge VALUES (%s,%s,%s,%s,%s,%s,%s)"
cur.execute(q,(pid,name,d_date,days,t_cost,r_charge,total))
cur.execute("DELETE FROM treatment WHERE patient_id=%s",(pid,))
cur.execute("DELETE FROM patient WHERE patient_id=%s",(pid,))
con.commit()
print("\n----- INVOICE -----")
bill = [[pid,name,days,t_cost,r_charge,total]]
print(tabulate(bill,headers=["PID","Name","Days","Treat Amt","Room Amt","Total"],tablefmt="grid"))
# ---------------- MAIN MENU ----------------
while True:
print("\n1.Add Patient\n2.Add Treatment\n3.Search Patient\n4.Modify Patient\n5.Delete Patient\n6.Show Records\n7.Discharge Patient\n8.Exit")
ch = int(input("Enter choice: "))
if ch==1:
add_patient()
elif ch==2:
add_treatment()
elif ch==3:
search_patient()
elif ch==4:
modify_patient()
elif ch==5:
delete_patient()
elif ch==6:
show_records()
elif ch==7:
discharge_patient()
elif ch==8:
break
else:
print("Invalid Choice")
📌 7. STEP-BY-STEP EXECUTION
- Install MySQL Server
- Install Python 3
- Install required modules
mysql-connector-python & tabulate, by running the following commands in command prompt, run cmd as administrator:
pip installmysql-connector-python
pip install tabulate - Run Python file
python hospital_management.py