Hospital Management - Py
Hospital Management - Py
def connect_db():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
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()
# 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
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()
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.")
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.")