0% found this document useful (0 votes)
11 views4 pages

Hospital Management - Py

Uploaded by

watermelon1434
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Hospital Management - Py

Uploaded by

watermelon1434
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import sqlite3

# Define the database file


DATABASE = 'hospital_management.db'

def connect_db():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn

# Function to check bed availability in a department of a specific hospital


'''def check_availability(hospital_name, department_name):
conn = connect_db()
cursor = conn.cursor()

cursor.execute(''
SELECT d.name AS department_name,
h.name AS hospital_name,
COUNT(b.id) - SUM(CASE WHEN b.is_occupied THEN 1 ELSE 0 END) AS
available_beds,
COUNT(q.id) AS queue_length,
COUNT(p.id) AS registered_patients
FROM Departments d
JOIN Hospitals h ON d.hospital_id = h.id
LEFT JOIN Beds b ON d.id = b.department_id
LEFT JOIN Queues q ON d.id = q.department_id
LEFT JOIN Patients p ON d.id = p.department_id
WHERE h.name = ? AND d.name = ?
GROUP BY d.id, h.name
'', (hospital_name, department_name))

result = cursor.fetchone()
if result:
print(f"Hospital: {result['hospital_name']}")
print(f"Department: {result['department_name']}")
print(f"Registered Patients: {result['registered_patients']}")
print(f"Available Beds: {result['available_beds'] if
result['available_beds'] is not None else 0}")
print(f"Queue Length: {result['queue_length'] if result['queue_length'] is
not None else 0}")
else:
print("Hospital or Department not found.")

conn.close()
'''
def check_availability(hospital_name, department_name):
conn = connect_db()
cursor = conn.cursor()

cursor.execute('''
SELECT d.name AS department_name,
h.name AS hospital_name,
COUNT(b.id) - SUM(CASE WHEN b.is_occupied THEN 1 ELSE 0 END) AS
available_beds,
COUNT(q.id) AS queue_length,
COUNT(p.id) AS registered_patients
FROM Departments d
JOIN Hospitals h ON d.hospital_id = h.id
LEFT JOIN Beds b ON d.id = b.department_id
LEFT JOIN Queues q ON d.id = q.department_id
LEFT JOIN Patients p ON d.id = p.department_id
WHERE h.name = ? AND d.name = ?
GROUP BY d.id, h.name
''', (hospital_name, department_name))

result = cursor.fetchone()
if result:
print(f"Hospital: {result['hospital_name']}")
print(f"Department: {result['department_name']}")
print(f"Registered Patients: {result['registered_patients']}")
print(f"Available Beds: {result['available_beds'] if
result['available_beds'] is not None else 0}")
print(f"Queue Length: {result['queue_length'] if result['queue_length'] is
not None else 0}")
else:
print(f"Hospital: '{hospital_name}' or Department: '{department_name}' not
found.")

conn.close()

# Function to admit a patient to a department of a specific hospital


def admit_patient(name, age, hospital_name, department_name):
conn = connect_db()
cursor = conn.cursor()

# Get Department ID
cursor.execute('''
SELECT d.id
FROM Departments d
JOIN Hospitals h ON d.hospital_id = h.id
WHERE h.name = ? AND d.name = ?
''', (hospital_name, department_name))
dept = cursor.fetchone()

if not dept:
print(f"Hospital: '{hospital_name}' or Department: '{department_name}' not
found.")
conn.close()
return

department_id = dept['id']

# Add Patient
cursor.execute('''
INSERT INTO Patients (name, age, department_id, status) VALUES (?, ?, ?, ?)
''', (name, age, department_id, 'Waiting'))
patient_id = cursor.lastrowid

# Check for available bed


cursor.execute('''
SELECT id FROM Beds WHERE department_id = ? AND is_occupied = 0 LIMIT 1
''', (department_id,))
bed = cursor.fetchone()

if bed:
# Assign bed to patient
bed_id = bed['id']
cursor.execute('''
UPDATE Beds SET is_occupied = 1, patient_id = ? WHERE id = ?
''', (patient_id, bed_id))
cursor.execute('''
UPDATE Patients SET status = 'Admitted' WHERE id = ?
''', (patient_id,))
conn.commit()
print(f"Patient admitted and assigned to bed {bed_id}.")
else:
# Add to queue
cursor.execute('''
SELECT MAX(position) FROM Queues WHERE department_id = ?
''', (department_id,))
max_pos = cursor.fetchone()['MAX(position)']
next_pos = (max_pos + 1) if max_pos else 1
cursor.execute('''
INSERT INTO Queues (department_id, patient_id, position) VALUES (?, ?, ?)
''', (department_id, patient_id, next_pos))
conn.commit()
print(f"No beds available. Patient added to queue at position {next_pos}.")

conn.close()

# Function to discharge a patient


def discharge_patient(patient_id):
conn = connect_db()
cursor = conn.cursor()

# Update patient status


cursor.execute('''
UPDATE Patients SET status = 'Discharged' WHERE id = ?
''', (patient_id,))

# Free the bed


cursor.execute('''
UPDATE Beds SET is_occupied = 0, patient_id = NULL WHERE patient_id = ?
''', (patient_id,))

# Check if there's a queue


cursor.execute('''
SELECT q.id, q.patient_id, q.position
FROM Queues q
WHERE q.department_id = (
SELECT department_id FROM Patients WHERE id = ?
)
ORDER BY q.position ASC LIMIT 1
''', (patient_id,))
next_in_queue = cursor.fetchone()

if next_in_queue:
queue_id, next_patient_id, position = next_in_queue
# Assign bed to next patient
cursor.execute('''
UPDATE Beds SET is_occupied = 1, patient_id = ? WHERE department_id = (
SELECT department_id FROM Patients WHERE id = ?
) AND is_occupied = 0 LIMIT 1
''', (next_patient_id, next_patient_id))
# Update patient status
cursor.execute('''
UPDATE Patients SET status = 'Admitted' WHERE id = ?
''', (next_patient_id,))
# Remove from queue
cursor.execute('''
DELETE FROM Queues WHERE id = ?
''', (queue_id,))

conn.commit()
conn.close()
print("Patient discharged successfully.")

# Simple command-line interface


if __name__ == "__main__":
while True:
print("\nHospital Management System")
print("1. Check Availability")
print("2. Admit Patient")
print("3. Discharge Patient")
print("4. Exit")
choice = input("Enter your choice: ")

if choice == '1':
hospital_name = input("Enter hospital name (e.g., City Hospital,
Greenwood Medical Center): ")
department_name = input("Enter department name (e.g., Cardiology, ENT,
Gastroenterology): ")
check_availability(hospital_name, department_name)
elif choice == '2':
name = input("Enter patient's name: ")
age = input("Enter patient's age: ")
hospital_name = input("Enter hospital name (e.g., City Hospital,
Greenwood Medical Center): ")
department_name = input("Enter department name (e.g., Cardiology, ENT,
Gastroenterology): ")
admit_patient(name, age, hospital_name, department_name)
elif choice == '3':
patient_id = int(input("Enter patient ID to discharge: "))
discharge_patient(patient_id)
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice, please try again.")

You might also like