-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
39 lines (32 loc) · 1.38 KB
/
database.py
File metadata and controls
39 lines (32 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sqlite3
class Database:
def __init__(self, name: str):
self.conn = sqlite3.connect(name + '.db', check_same_thread=False)
self.cursor = self.conn.cursor()
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id TEXT NOT NULL,
name TEXT NOT NULL,
class_name TEXT NOT NULL,
arrived BLOB NOT NULL,
image_link TEXT
)
''')
def add_user(self, uid: str, name: str, class_name: str, image_link: str):
self.cursor.execute('''
INSERT INTO users (id, name, class_name, arrived, image_link) VALUES (?, ?, ?, ?, ?)
''', (uid, name, class_name, False, image_link))
self.conn.commit()
def get_user(self, uid: str):
self.cursor.execute('SELECT name, class_name, arrived, image_link FROM users WHERE id = ?', (uid,))
results = self.cursor.fetchall()
if len(results) == 0:
return {"exists": False}
user = results[0]
return {"exists": True, "name": user[0], "class_name": user[1], "arrived": user[2], "image_link": user[3]}
def mark_user_as_arrived(self, uid: str):
self.cursor.execute('UPDATE users SET arrived = ? WHERE id = ?', (True, uid))
self.conn.commit()
def mark_all_users_as_not_arrived(self):
self.cursor.execute('UPDATE users SET arrived = ?', (False,))
self.conn.commit()