This repository was archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
63 lines (57 loc) · 1.93 KB
/
database.py
File metadata and controls
63 lines (57 loc) · 1.93 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
from datetime import datetime
DATABASE_NAME = 'submissions.db'
def get_db_connection():
conn = sqlite3.connect(DATABASE_NAME)
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS submissions (
id INTEGER PRIMARY KEY,
filename TEXT NOT NULL,
code TEXT NOT NULL,
submission_date TEXT NOT NULL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS fingerprints (
id INTEGER PRIMARY KEY,
submission_id INTEGER,
position INTEGER,
hash INTEGER,
FOREIGN KEY (submission_id) REFERENCES submissions (id)
)
''')
conn.commit()
conn.close()
def save_submission(filename, code, fingerprints):
conn = get_db_connection()
cursor = conn.cursor()
submission_date = datetime.now().isoformat()
try:
cursor.execute('INSERT INTO submissions (filename, code, submission_date) VALUES (?, ?, ?)',
(filename, code, submission_date))
submission_id = cursor.lastrowid
cursor.executemany('INSERT INTO fingerprints (submission_id, position, hash) VALUES (?, ?, ?)',
[(submission_id, position, hash_value) for position, hash_value in fingerprints])
conn.commit()
except sqlite3.Error as e:
print(f"An error occurred: {e}")
conn.rollback()
finally:
conn.close()
def get_all_submissions():
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute('SELECT id, filename, submission_date FROM submissions ORDER BY submission_date DESC')
submissions = cursor.fetchall()
return submissions
except sqlite3.Error as e:
print(f"An error occurred: {e}")
return []
finally:
conn.close()