-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
63 lines (52 loc) · 1.58 KB
/
database.py
File metadata and controls
63 lines (52 loc) · 1.58 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sqlite3
DB_NAME = "nero_attendance.db"
def get_connection():
return sqlite3.connect(DB_NAME)
def init_db():
conn = None
try:
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY,
student_id TEXT UNIQUE,
name TEXT,
face_encoding BLOB,
photo_data BLOB
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS exam_sessions (
id INTEGER PRIMARY KEY,
exam_code TEXT UNIQUE,
subject TEXT,
exam_date TEXT,
exam_time TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS attendance_roster (
id INTEGER PRIMARY KEY,
student_id TEXT,
exam_code TEXT,
table_number INTEGER,
status TEXT DEFAULT 'Absent',
timestamp DATETIME
)
"""
)
# Backfill schema for existing databases created before photo_data existed.
col_rows = cursor.execute("PRAGMA table_info(students)").fetchall()
existing_cols = {row[1] for row in col_rows}
if "photo_data" not in existing_cols:
cursor.execute("ALTER TABLE students ADD COLUMN photo_data BLOB")
conn.commit()
finally:
if conn is not None:
conn.close()