-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbHelper.py
More file actions
136 lines (116 loc) · 6.61 KB
/
dbHelper.py
File metadata and controls
136 lines (116 loc) · 6.61 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
This file is to assist the flask application with reading and writing the database.
It will increase the readability of the flask file.
"""
import sqlite3
from sqlite3 import Error
class Main:
"""
This main class of the database helper is:
- to execute all types of queries
- read a table in the database
- delete an entry in a table in the database
"""
def execute_query(self, query_list, commit=False, fetchAll=False, fetchOne=False):
try:
conn = sqlite3.connect("comments.db")
c = conn.cursor()
result = None
if type(query_list) == str:
c.execute(query_list)
elif isinstance(query_list, tuple):
c.execute(query_list[0], query_list[1])
else:
for query in query_list:
if isinstance(query, tuple):
c.execute(query[0], query[1])
else:
c.execute(query)
if commit:
conn.commit()
if fetchAll:
result = [row for row in c.fetchall()]
if fetchOne:
result = c.fetchone()
c.close()
conn.close()
return result
except Error as e:
print(e)
def read_database(self, db_name):
return self.execute_query(query_list=f"SELECT * FROM {db_name}", fetchAll=True)
def read_columns(self, db_name):
data = self.execute_query(query_list=f"PRAGMA table_info({db_name})", fetchAll=True)
return [x[1] for x in data]
def del_comment(self, id, db_name):
self.execute_query(query_list=f'DELETE FROM {db_name} WHERE id = {id}', commit=True)
class Comment(Main):
"""
This child class is to add and delete comments in the comments table.
"""
def __init__(self, comment_arg, type_arg, written_arg, math_arg, programming_arg, grade_of_excellence_arg):
self.comment = comment_arg
self.type_comment = type_arg
self.written = written_arg
self.math = math_arg
self.programming = programming_arg
self.grade_of_excellence = grade_of_excellence_arg
def __str__(self):
return f"{self.comment}, {self.type_comment}, {int(self.written)}, {int(self.math)}, {int(self.programming)}, {self.grade_of_excellence}."
def add_comment(self):
query1 = """CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY AUTOINCREMENT, comment TEXT NOT NULL, type_comment TEXT NOT NULL, written INTEGER NOT NULL, math INTEGER NOT NULL, programming INTEGER NOT NULL, grade_of_excellence TEXT NOT NULL)"""
query2 = "INSERT INTO comments (comment, type_comment, written, math, programming, grade_of_excellence) VALUES (?,?,?,?,?,?)", (
self.comment, self.type_comment, int(self.written), int(self.math), int(self.programming),
self.grade_of_excellence)
self.execute_query(query_list=[query1, query2], commit=True)
def update_comment(self, id):
self.execute_query(query_list=[('UPDATE comments SET comment = ? WHERE id = ?', (self.comment, id))],
commit=True)
self.execute_query(query_list=[('UPDATE comments SET type_comment = ? WHERE id = ?', (self.type_comment, id))],
commit=True)
self.execute_query(query_list=[('UPDATE comments SET written = ? WHERE id = ?', (self.written, id))],
commit=True)
self.execute_query(query_list=[('UPDATE comments SET math = ? WHERE id = ?', (self.math, id))], commit=True)
self.execute_query(query_list=[('UPDATE comments SET programming = ? WHERE id = ?', (self.programming, id))],
commit=True)
self.execute_query(
query_list=[('UPDATE comments SET grade_of_excellence = ? WHERE id = ?', (self.grade_of_excellence, id))],
commit=True)
class Requirements(Main):
"""
This child class is to add and delete requirements in the requirements table.
"""
def __init__(self, requirement_arg, best_arg, good_arg, bad_arg, worst_arg, written_arg, programming_arg, math_arg,
type_arg):
self.requirement = requirement_arg
self.best = best_arg
self.good = good_arg
self.bad = bad_arg
self.worst = worst_arg
self.written = written_arg
self.programming = programming_arg
self.math = math_arg
self.template_type = type_arg
def __str__(self):
return f"{self.requirement}, {self.best}, {self.good},{self.bad}, {self.worst}, {self.written},{self.programming}, {self.math}, {self.template_type}"
def add_requirement(self):
query1 = """CREATE TABLE IF NOT EXISTS requirements (id INTEGER PRIMARY KEY AUTOINCREMENT, requirement TEXT NOT NULL, best TEXT NOT NULL, good TEXT NOT NULL, bad TEXT NOT NULL, worst TEXT NOT NULL, written INTEGER NOT NULL, math INTEGER NOT NULL, programming INTEGER NOT NULL, type TEXT NOT NULL)"""
query2 = "INSERT INTO requirements (requirement, best, good, bad, worst, written, math, programming, type) VALUES (?,?,?,?,?,?,?,?,?)", (
self.requirement, self.best, self.good, self.bad, self.worst, int(self.written), int(self.math),
int(self.programming), self.template_type)
self.execute_query(query_list=[query1, query2], commit=True)
def update_requirement(self, id):
self.execute_query(
query_list=[('UPDATE requirements SET requirement = ? WHERE id = ?', (self.requirement, id))], commit=True)
self.execute_query(query_list=[('UPDATE requirements SET best = ? WHERE id = ?', (self.best, id))], commit=True)
self.execute_query(query_list=[('UPDATE requirements SET good = ? WHERE id = ?', (self.good, id))], commit=True)
self.execute_query(query_list=[('UPDATE requirements SET bad = ? WHERE id = ?', (self.bad, id))], commit=True)
self.execute_query(query_list=[('UPDATE requirements SET worst = ? WHERE id = ?', (self.worst, id))],
commit=True)
self.execute_query(query_list=[('UPDATE requirements SET written = ? WHERE id = ?', (self.written, id))],
commit=True)
self.execute_query(query_list=[('UPDATE requirements SET math = ? WHERE id = ?', (self.math, id))], commit=True)
self.execute_query(
query_list=[('UPDATE requirements SET programming = ? WHERE id = ?', (self.programming, id))], commit=True)
self.execute_query(query_list=[('UPDATE requirements SET type = ? WHERE id = ?', (self.template_type, id))],
commit=True)