import sqlite3
# Define the database file
DATABASE = 'hospital_management.db'
# Connect to the SQLite database (or create it if it doesn't exist)
conn = [Link](DATABASE)
conn.row_factory = [Link]
cursor = [Link]()
# Drop existing tables if they exist
def drop_tables():
[Link]('DROP TABLE IF EXISTS Queues')
[Link]('DROP TABLE IF EXISTS Beds')
[Link]('DROP TABLE IF EXISTS Patients')
[Link]('DROP TABLE IF EXISTS Departments')
[Link]('DROP TABLE IF EXISTS Hospitals')
[Link]()
# Create the required tables
def init_db():
drop_tables() # Ensure tables are recreated
[Link]('''
CREATE TABLE IF NOT EXISTS Hospitals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
address TEXT
)
''')
[Link]('''
CREATE TABLE IF NOT EXISTS Departments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
hospital_id INTEGER,
FOREIGN KEY(hospital_id) REFERENCES Hospitals(id)
)
''')
[Link]('''
CREATE TABLE IF NOT EXISTS Patients (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
department_id INTEGER,
status TEXT,
FOREIGN KEY(department_id) REFERENCES Departments(id)
)
''')
[Link]('''
CREATE TABLE IF NOT EXISTS Beds (
id INTEGER PRIMARY KEY AUTOINCREMENT,
department_id INTEGER,
is_occupied BOOLEAN DEFAULT 0,
patient_id INTEGER,
FOREIGN KEY(department_id) REFERENCES Departments(id),
FOREIGN KEY(patient_id) REFERENCES Patients(id)
)
''')
[Link]('''
CREATE TABLE IF NOT EXISTS Queues (
id INTEGER PRIMARY KEY AUTOINCREMENT,
department_id INTEGER,
patient_id INTEGER,
position INTEGER,
FOREIGN KEY(department_id) REFERENCES Departments(id),
FOREIGN KEY(patient_id) REFERENCES Patients(id)
)
''')
[Link]()
[Link]()
if __name__ == "__main__":
init_db()