Skip to content
Snippets Groups Projects
Commit 9c1e29bc authored by Timon Dörzapf's avatar Timon Dörzapf
Browse files

Implemented database

parent a64916f1
Branches
No related tags found
No related merge requests found
import sqlite3
import db_data
DB_NAME = 'dispenser.db'
class DB():
def __init__(self):
self.db = sqlite3.connect(DB_NAME, check_same_thread=False)
self.cursor = self.db.cursor()
self.cursor.execute(db_data.CREATE_DISPENSES)
# Create patients data
self.cursor.execute(db_data.CREATE_PATIENTS)
self.db.commit()
def reset(self):
self.cursor.execute(db_data.DROP_DISPENSES)
self.cursor.execute(db_data.CREATE_DISPENSES)
self.cursor.execute(db_data.DROP_PATIENTS)
self.cursor.execute(db_data.CREATE_PATIENTS)
for patient in db_data.PATIENS:
self.cursor.execute(db_data.INSERT_PATIENT, patient)
self.db.commit()
def dispense(self, patient_id, time):
self.cursor.execute(db_data.INSERT_DISPENSE, (patient_id, time))
self.db.commit()
def select(self):
self.cursor.execute('''
SELECT * FROM dispenses
''')
return self.cursor.fetchall()
def delete(self, id):
self.cursor.execute('''
DELETE FROM dispenses
WHERE id = ?
''', (id,))
self.db.commit()
def clear(self):
self.cursor.execute('''
DELETE FROM dispenses
''')
# Remove dispenses table
self.cursor.execute(db_data.DROP_DISPENSES)
self.db.commit()
\ No newline at end of file
CREATE_DISPENSES = '''
CREATE TABLE IF NOT EXISTS dispenses(
id INTEGER PRIMARY KEY,
patient_id INTEGER,
time TEXT
)
'''
CREATE_PATIENTS = '''
CREATE TABLE IF NOT EXISTS patients(
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER,
weight INTEGER,
height INTEGER
)
'''
DROP_DISPENSES = '''
DROP TABLE IF EXISTS dispenses
'''
DROP_PATIENTS = '''
DROP TABLE IF EXISTS patients
'''
INSERT_PATIENT = '''
INSERT INTO patients(name, age, weight, height)
VALUES(?, ?, ?, ?)
'''
INSERT_DISPENSE = '''
INSERT INTO dispenses(patient_id, time)
VALUES(?, ?)
'''
PATIENS = [
('John Doe', 25, 70, 170),
('Jane Doe', 30, 60, 160),
('John Smith', 40, 80, 180),
('Jane Smith', 35, 65, 165),
('John Brown', 45, 85, 175),
('Jane Brown', 50, 55, 155)
]
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment