-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysqlConnector.py
316 lines (275 loc) · 12.5 KB
/
mysqlConnector.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import mysql.connector
class MySQLClient:
def __init__(self, host, user, password, database):
self.host = host
self.user = user
self.password = password
self.database = database
self.connection = mysql.connector.connect(host=host,
user=user,
password=password,
database=database,
auth_plugin='mysql_native_password')
def converByteStringToString(self, byteString):
return byteString.decode("utf-8")
# Queries for databases
def showDatabases(self):
cursor = self.connection.cursor()
# Execute the query
cursor.execute("SHOW DATABASES")
return cursor.fetchall()
def useDatabase(self, dbName):
# Enter to database
cursor = self.connection.cursor()
# Execute the query
cursor.execute("USE {}".format(dbName))
def showTables(self, dbName):
# Show tabales inside the database
cursor = self.connection.cursor()
# Execute the query
cursor.execute('USE {}'.format(dbName))
cursor.execute('SHOW TABLES')
return cursor.fetchall()
def readDataFromTable(self, dbName, tableName):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute('SELECT * FROM {}.{}'.format(dbName, tableName))
return cursor.fetchall()
# Entering data to the table
def insert_data(self, tableName, idStudents, Password, StudentUsername, student_Index):
cursor = self.connection.cursor()
# Execute the query
query = """INSERT INTO `{}` (idStudents,Password,StudentUsername,student_Index) VALUES (%s,%s,%s,%s);""".format(
tableName)
val = (idStudents, Password, StudentUsername, student_Index)
cursor.execute(query, val)
self.connection.commit()
# Entering data to the application table
def insert_applicationData(self, tableName, idapplications, request_status, Details, evidence, filename, from_id, to_id, requestType, today, studentReaded, staffReaded, required):
cursor = self.connection.cursor()
# Execute the query
query = """INSERT INTO `{}` (idapplications,request_status,Details,evidence,filename,from_id,to_id,requestType,date,studentReaded, staffReaded,required) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);""".format(
tableName)
val = (idapplications, request_status, Details,
evidence, filename, from_id, to_id, requestType, today, studentReaded, staffReaded, required)
cursor.execute(query, val)
self.connection.commit()
# Entering data to the comments table
def insert_commentData(self, tableName, idComments, commentUserType, content, idThreads, dateTime):
cursor = self.connection.cursor()
# Execute the query
query = """INSERT INTO `{}` (idComments, commentUserType, content, idThreads, dateTime) VALUES (%s,%s,%s,%s,%s);""".format(
tableName)
val = (idComments, commentUserType, content, idThreads, dateTime)
cursor.execute(query, val)
self.connection.commit()
def update_Studentdata(self, tableName, StudentUsername, Password):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET Password=%s WHERE StudentUsername=%s;""".format(
tableName)
val = (Password, StudentUsername)
cursor.execute(query, val)
self.connection.commit()
def removeEvidence(self, tableName, idapplications):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET evidence = '', filename = '' WHERE idapplications=%s;""".format(
tableName)
val = (idapplications,)
cursor.execute(query, val)
self.connection.commit()
def updateEvidence(self, tableName, idapplications, evidence, filename):
cursor = self.connection.cursor()
# Execute the query
#strId = str(idapplications)
query = """UPDATE `{}` SET evidence = %s, filename = %s WHERE idapplications=%s;""".format(
tableName)
val = (evidence, filename, idapplications)
cursor.execute(query, val)
self.connection.commit()
def update_Userdata(self, tableName, UserName, UserPassword):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET UserPassword=%s WHERE UserName=%s;""".format(
tableName)
val = (UserPassword, UserName)
cursor.execute(query, val)
self.connection.commit()
def updateApplicationStatus(self, tableName, idapplications, request_status):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET request_status=%s WHERE idapplications=%s;""".format(
tableName)
val = (request_status, idapplications)
cursor.execute(query, val)
self.connection.commit()
def updateApplicationStudentRead(self, tableName, idapplications, studentReaded):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET studentReaded=%s WHERE idapplications=%s;""".format(
tableName)
val = (studentReaded, idapplications)
cursor.execute(query, val)
self.connection.commit()
def updateApplicationStaffRead(self, tableName, idapplications, staffReaded):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET staffReaded=%s WHERE idapplications=%s;""".format(
tableName)
val = (staffReaded, idapplications)
cursor.execute(query, val)
self.connection.commit()
def updateCommentsThreadId(self, tableName, previousId, newId):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET idThreads=%s WHERE idThreads=%s;""".format(
tableName)
val = (newId, previousId)
cursor.execute(query, val)
self.connection.commit()
def updateApplicationMore(self, tableName, idapplications, required):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET required=%s WHERE idapplications=%s;""".format(
tableName)
val = (required, idapplications)
cursor.execute(query, val)
self.connection.commit()
def updateApplicationDetails(self, tableName, idapplications, Details):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET Details=%s WHERE idapplications=%s;""".format(
tableName)
val = (Details, idapplications)
cursor.execute(query, val)
self.connection.commit()
def updateApplicationRead(self, tableName, idapplications, readed):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET readed=%s WHERE idapplications=%s;""".format(
tableName)
val = (readed, idapplications)
cursor.execute(query, val)
self.connection.commit()
def update_Staffdata(self, tableName, username, password):
cursor = self.connection.cursor()
# Execute the query
query = """UPDATE `{}` SET password=%s WHERE username=%s;""".format(
tableName)
val = (password, username)
cursor.execute(query, val)
self.connection.commit()
def deleteApplication(self, tableName, idapplications):
cursor = self.connection.cursor()
query = """DELETE FROM `{}` WHERE idapplications=%s;""".format(
tableName)
val = (idapplications,)
cursor.execute(query, val)
self.connection.commit()
def searchDataFromStudentTable(self, tableName, StudentUsername):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE StudentUsername='{}'".format(tableName, StudentUsername))
return cursor.fetchall()
def searchDataFromIdUsingStudentTable(self, tableName, idStudents):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE idStudents='{}'".format(tableName, idStudents))
return cursor.fetchall()
def searchDataFromIdUsingStaffTable(self, tableName, idadministrators):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE idadministrators='{}'".format(tableName, idadministrators))
return cursor.fetchall()
def searchDataFromIdThreadsUsingCommentTable(self, tableName, idThreads):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE idThreads='{}'".format(tableName, idThreads))
return cursor.fetchall()
def searchDataFromStaffTable(self, tableName, username):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE username='{}'".format(tableName, username))
return cursor.fetchall()
def searchDataFromUserTable(self, tableName, UserName):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE UserName='{}'".format(tableName, UserName))
return cursor.fetchall()
def searchDataFromApplicationTable(self, tableName, idapplications):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE idapplications='{}'".format(tableName, idapplications))
return cursor.fetchall()
def searchRelatedDataStaffApplicationTable(self, tableName, to_id):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE to_id='{}'".format(tableName, to_id))
return cursor.fetchall()
def searchRelatedDataStudentApplicationTable(self, tableName, from_id):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE from_id='{}'".format(tableName, from_id))
return cursor.fetchall()
def searchStudentRelatedApplicationIds(self, tableName, from_id):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT idapplications FROM {} WHERE from_id='{}'".format(tableName, from_id))
return cursor.fetchall()
def searchStaffRelatedApplicationIds(self, tableName, to_id):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT idapplications FROM {} WHERE to_id='{}'".format(tableName, to_id))
return cursor.fetchall()
def searchRelatedDataApplicationApplicationTable(self, tableName, idapplications):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
"SELECT * FROM {} WHERE idapplications='{}'".format(tableName, idapplications))
return cursor.fetchall()
def convertToBinaryData(self, filename):
# Convert digital data to binary format
with open(filename, 'rb') as file:
binaryData = file.read()
return (binaryData, filename)
def insert_filetest(self, tableName, id, name, filecontent, filename):
cursor = self.connection.cursor()
# Execute the query
query = """INSERT INTO `{}` (`idapplications`, `name`, `filetab`,`filename`) VALUES (%s,%s,%s,%s);""".format(
tableName)
val = (id, name, filecontent, filename)
cursor.execute(query, val)
self.connection.commit()
def dowloadfile(self, tableName, idapplications):
# Read data from a table
cursor = self.connection.cursor()
# Execute the query
cursor.execute(
'SELECT * FROM {} WHERE idapplications = {}'.format(tableName, idapplications))
current_row = cursor.fetchall()[0]
return current_row[3], current_row[4]