-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
121 lines (90 loc) · 2.84 KB
/
database.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sqlite3
conn = sqlite3.connect("database.db")
conn.execute("PRAGMA foreign_keys = 1")
cur = conn.cursor()
# Create 2 tables if they don't exist: Exams and Match
cur.execute(
"""CREATE TABLE IF NOT EXISTS Exams
(EID INTEGER PRIMARY KEY AUTOINCREMENT,
question_name VARCHAR,
question VARCHAR,
MistakesNumber INTEGER,
user_answer VARCHAR,
CorrectAnswer VARCHAR,
Created DATETIME DEFAULT CURRENT_TIMESTAMP,
finished DATETIME DEFAULT NULL)"""
)
cur.execute(
"""CREATE TABLE IF NOT EXISTS Match
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
ErrorText VARCHAR,
ErrorOffset INTEGER,
ErrorLength INTEGER,
Context VARCHAR,
Category VARCHAR,
ruleIssue VARCHAR,
sentence VARCHAR,
ruleID VARCHAR,
numberSuggestions INTEGER,
Suggestions VARCHAR,
EID INT,
CONSTRAINT fk_exam
FOREIGN KEY (EID) REFERENCES Exams (EID)
ON DELETE CASCADE)
"""
)
conn.commit()
# create an instance of Exams table and return its id
def insert_exam(entities):
cur.execute(
"INSERT INTO Exams(question_name, question, MistakesNumber, user_answer, CorrectAnswer, Created, finished) VALUES(?, ?, ?, ?, ?, ?, ?)",
entities,
)
conn.commit()
return cur.lastrowid
# return a list of user's exams
def fetch_exams():
cur.execute("SELECT * FROM Exams")
rows = cur.fetchall()
exams = list()
for row in rows:
exams.append(row)
return exams
# delete a Exam by its id
def delete_exam(pk):
cur.execute("DELETE FROM Exams WHERE EID= " + str(pk))
# matches = fetch_matches(pk)
# for match in matches:
# delete_match(match.id)
conn.commit()
# create an instance of Match table
def insert_match(entities):
cur.execute(
"INSERT INTO Match(ErrorText, ErrorOffset, ErrorLength, Context, Category, ruleIssue, sentence, ruleID, numberSuggestions, Suggestions, EID) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
entities,
)
conn.commit()
# delete a match by its id
def delete_match(pk):
cur.execute("DELETE FROM Match WHERE id= " + str(pk))
conn.commit()
# return a list of the matches of a specific exam using EID
def fetch_matches(fk):
cur.execute("SELECT * FROM Match WHERE EID = " + str(fk))
rows = cur.fetchall()
matches = list()
for row in rows:
matches.append(row)
return matches
# return number of occurence of a word in the selected exam
def number_of_suggestions(exam_id, word):
cur.execute(f"SELECT Suggestions FROM Match WHERE EID = '{exam_id}' AND Suggestions LIKE '%{word}%' AND numberSuggestions = 1")
rows = cur.fetchall()
return len(rows)
# return a dictionary of each ruleIssue and number of its occurence
def number_of_ruleIssues(exam_id):
cur.execute(f"SELECT ruleIssue, COUNT(*) as COUNT from Match GROUP BY ruleIssue")
rows = cur.fetchall()
return dict(rows)
# d = {'misspelling': 2, 'typographical': 1}
# print(d['misspelling'])