0% found this document useful (0 votes)
8 views8 pages

Hospital Management System Code

CBSE CLASS 12 COMPUTER SCIENCE SOURCE CODE USING PYTHON AND MYSQL | HOSPITAL MANAGEMENT SYSTEM | EASY TO LEARN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Hospital Management System Code

CBSE CLASS 12 COMPUTER SCIENCE SOURCE CODE USING PYTHON AND MYSQL | HOSPITAL MANAGEMENT SYSTEM | EASY TO LEARN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

# HOSPITAL MANAGEMENT SYSTEM

import [Link]

# -------------------------------

# Database Connection

# -------------------------------

con = [Link](

host="localhost",

user="root",

password="mysql",

database="hospital_db"

cur = [Link]()

# -------------------------------

# Create Table if not exists

# -------------------------------

[Link]("""

CREATE TABLE IF NOT EXISTS patients (

patient_id VARCHAR(10) PRIMARY KEY,

name VARCHAR(50),

age INT,

gender VARCHAR(10),

disease VARCHAR(100),
phone VARCHAR(15)

""")

[Link]()

# -------------------------------

# Menu

# -------------------------------

def menu():

print("\n==============================")

print(" HOSPITAL MANAGEMENT SYSTEM ")

print("==============================")

print("1. Add New Patient Record")

print("2. Display All Patients")

print("3. Search Patient by ID")

print("4. Modify Patient Details")

print("5. Delete Patient Record")

print("6. Exit")

print("==============================")

# -------------------------------

# Add Patient

# -------------------------------

def add_patient():

pid = input("Enter Patient ID: ")


name = input("Enter Name: ")

age = int(input("Enter Age: "))

gender = input("Enter Gender: ")

disease = input("Enter Disease/Symptoms: ")

phone = input("Enter Phone Number: ")

try:

[Link]("INSERT INTO patients VALUES (%s, %s, %s, %s, %s, %s)",

(pid, name, age, gender, disease, phone))

[Link]()

print("Patient record added successfully!")

except [Link]:

print(" Error: Patient ID already exists!")

# -------------------------------

# Display All Patients

# -------------------------------

def display_patients():

[Link]("SELECT * FROM patients")

rows = [Link]()

if not rows:

print("No records found.")

else:

print("\n--- All Patient Records ---")

print(f"{'ID':<10}{'Name':<15}{'Age':<5}{'Gender':<10}{'Disease':<20}{'Phone'}")
print("-" * 70)

for r in rows:

print(f"{r[0]:<10}{r[1]:<15}{r[2]:<5}{r[3]:<10}{r[4]:<20}{r[5]}")

# -------------------------------

# Search Patient

# -------------------------------

def search_patient():

pid = input("Enter Patient ID to search: ")

[Link]("SELECT * FROM patients WHERE patient_id=%s", (pid,))

r = [Link]()

if r:

print("\n--- Patient Found ---")

print(f"ID: {r[0]}")

print(f"Name: {r[1]}")

print(f"Age: {r[2]}")

print(f"Gender: {r[3]}")

print(f"Disease: {r[4]}")

print(f"Phone: {r[5]}")

else:

print("Patient not found.")

# -------------------------------

# Modify Patient Details

# -------------------------------
def modify_patient():

pid = input("Enter Patient ID to modify: ")

[Link]("SELECT * FROM patients WHERE patient_id=%s", (pid,))

r = [Link]()

if not r:

print("Patient not found.")

return

print("\n--- Current Details ---")

print(f"1. Name: {r[1]}")

print(f"2. Age: {r[2]}")

print(f"3. Gender: {r[3]}")

print(f"4. Disease: {r[4]}")

print(f"5. Phone: {r[5]}")

choice = input("Enter the number of the field to modify (1-5): ")

if choice == '1':

new_value = input("Enter new Name: ")

[Link]("UPDATE patients SET name=%s WHERE patient_id=%s", (new_value, pid))

elif choice == '2':

new_value = int(input("Enter new Age: "))

[Link]("UPDATE patients SET age=%s WHERE patient_id=%s", (new_value, pid))

elif choice == '3':

new_value = input("Enter new Gender: ")


[Link]("UPDATE patients SET gender=%s WHERE patient_id=%s", (new_value, pid))

elif choice == '4':

new_value = input("Enter new Disease: ")

[Link]("UPDATE patients SET disease=%s WHERE patient_id=%s", (new_value, pid))

elif choice == '5':

new_value = input("Enter new Phone: ")

[Link]("UPDATE patients SET phone=%s WHERE patient_id=%s", (new_value, pid))

else:

print("Invalid choice.")

return

[Link]()

print("Patient details updated successfully!")

# -------------------------------

# Delete Patient

# -------------------------------

def delete_patient():

pid = input("Enter Patient ID to delete: ")

[Link]("SELECT * FROM patients WHERE patient_id=%s", (pid,))

r = [Link]()

if not r:

print(" Patient not found.")

return
confirm = input("Are you sure you want to delete this record? (y/n): ")

if [Link]() == 'y':

[Link]("DELETE FROM patients WHERE patient_id=%s", (pid,))

[Link]()

print("🗑 Patient record deleted successfully!")

# -------------------------------

# Main Program

# -------------------------------

while True:

menu()

ch = input("Enter your choice (1-6): ")

if ch == '1':

add_patient()

elif ch == '2':

display_patients()

elif ch == '3':

search_patient()

elif ch == '4':

modify_patient()

elif ch == '5':

delete_patient()

elif ch == '6':

print("Exiting program. Stay healthy!")


break

else:

print("Invalid choice! Please enter 1-6.")

[Link]()

You might also like