0% found this document useful (0 votes)
6 views

setup_database.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)
6 views

setup_database.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/ 2

import sqlite3

# Define the database file


DATABASE = 'hospital_management.db'

# Connect to the SQLite database (or create it if it doesn't exist)


conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()

# Drop existing tables if they exist


def drop_tables():
cursor.execute('DROP TABLE IF EXISTS Queues')
cursor.execute('DROP TABLE IF EXISTS Beds')
cursor.execute('DROP TABLE IF EXISTS Patients')
cursor.execute('DROP TABLE IF EXISTS Departments')
cursor.execute('DROP TABLE IF EXISTS Hospitals')
conn.commit()

# Create the required tables


def init_db():
drop_tables() # Ensure tables are recreated

cursor.execute('''
CREATE TABLE IF NOT EXISTS Hospitals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
address TEXT
)
''')

cursor.execute('''
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)
)
''')

cursor.execute('''
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)
)
''')

cursor.execute('''
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)
)
''')

cursor.execute('''
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)
)
''')

conn.commit()
conn.close()

if __name__ == "__main__":
init_db()

You might also like