-
Notifications
You must be signed in to change notification settings - Fork 0
/
MYSQLBdd.py
490 lines (409 loc) · 15.8 KB
/
MYSQLBdd.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# coding: utf-8
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 26 15:32:58 2018
@author: Administrator
"""
#Ce fichier gère toutes les requetes à la base de données
import mysql.connector
from mysql.connector import errorcode
import csv
import os
import datetime
#VARS
#get the tokens to get access to Slack, port number and public url to access the server
for line in open("tokens.txt", "r").readlines():
if "dbURL =" in line:
dbURL = line[line.index('='):][1:].replace("\n","")
if "baseSchemaName =" in line:
schName = line[line.index('='):][1:].replace("\n","")
if "userDB =" in line:
userName = line[line.index('='):][1:].replace("\n","")
if "portDB =" in line:
PORT = line[line.index('='):][1:].replace("\n","")
if "passwordDB =" in line:
pswDB = line[line.index('='):][1:].replace("\n","")
SQLtypes = ["TEXT", "TINYTEXT", "INT"]
CWD = os.getcwd()
"""
print(dbURL, schName, userName, PORT, pswDB)
"""
#tables c'est un dictionnaire contenant le nom des tables en clé et leurs descriptions en valeurs
tables = {}
tables ["Mood"] = (
"CREATE TABLE `Mood` ("
"`id` bigint (32) AUTO_INCREMENT,"
" `user` varchar(255) NOT NULL,"
" `humeur` varchar(255) NOT NULL,"
" `intensite` int(1) ,"
"`humeurSTR` varchar(255),"
"`dateSTR` varchar(255),"
" PRIMARY KEY (`id`)"
") ENGINE=InnoDB" )
tables ["Citation"] = (
"CREATE TABLE `Citation` ("
"`id` bigint (32) AUTO_INCREMENT,"
" `quote` TEXT NOT NULL,"
" `author` varchar(255) NOT NULL,"
" PRIMARY KEY (`id`)"
") ENGINE=InnoDB" )
tables ["Blague"] = (
"CREATE TABLE `Blague` ("
"`id` bigint (32) AUTO_INCREMENT,"
" `text` TEXT NOT NULL,"
" PRIMARY KEY (`id`)"
") ENGINE=InnoDB" )
tables ["Devinette"] = (
"CREATE TABLE `Devinette` ("
"`id` bigint (32) AUTO_INCREMENT,"
" `quote` TEXT NOT NULL,"
" `answer` varchar(255) NOT NULL,"
" PRIMARY KEY (`id`)"
") ENGINE=InnoDB" )
tables ["Video"] = (
"CREATE TABLE `Video` ("
"`id` bigint (32) AUTO_INCREMENT,"
" `url` varchar(255) NOT NULL,"
" `shot_description` varchar(255) NOT NULL,"
" PRIMARY KEY (`id`)"
") ENGINE=InnoDB" )
tables ["Image"] = (
"CREATE TABLE `Image` ("
"`id` bigint (32) AUTO_INCREMENT,"
" `url` varchar(255) NOT NULL,"
" `shot_description` varchar(255) NOT NULL,"
" PRIMARY KEY (`id`)"
") ENGINE=InnoDB" )
class monSql :
def __init__ (self,
http = dbURL,
base = schName,
user = userName,
port = PORT,
password = pswDB,
) :
self.cnx = mysql.connector.connect(user=user, password=password,
host=http, port=port,
)
self.cursor = self.cnx.cursor()
# on regarde si la base existe et sinon on cree
try:
self.cnx.database = base
except mysql.connector.Error as err:
if err.errno == errorcode.ER_BAD_DB_ERROR:
self.create_database(base)
self.cnx.database = base
else:
raise
def create_database(self, base):
try:
self.cursor.execute(
"CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(base))
except mysql.connector.Error as err:
print("Failed creating database: {}".format(err))
raise
return
def close (self,) :
self.cnx.close()
def getFieldTable(self ,table , schema = "test_chatbot" ):
liste_item = []
requete = """SELECT COLUMN_NAME AS `Field`
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = '{}' AND TABLE_NAME = '{}'
;""".format(schema , table)
self.cursor.execute(requete)
tmp = self.cursor.fetchall()
for i in tmp :
liste_item.append(i[0])
return liste_item[1:]
#retun all rows of the table
def getItemIntoTable(self, table):
liste_item = []
requete = """SELECT * FROM {}""".format(table)
try:
self.cursor.execute(requete)
liste_item = self.cursor.fetchall()
except mysql.connector.Error as err:
print("Failed retrieving database: {}".format(err))
raise
return liste_item
#insert a row in a table
def insert_IntoTable(self ,table , liste_item , liste_value):
liste_value = [mysqlStringPP(val) if type(val) == str else val for val in liste_value]
"""print(liste_value)
"""
requete = "INSERT INTO `{}` ".format(table)
requete +="("
for i in range(len(liste_item)) :
if liste_item[-1] == liste_item[i] :
requete = requete +liste_item[-1]
else:
requete = requete + liste_item[i] + ','
requete +=") VALUES ("
for j in range(len(liste_value)) :
if liste_value[j] == liste_value[-1]:
requete = requete + "'" + liste_value[j] + "'"
else :
requete = requete + "'" + liste_value[j] + "',"
requete += ");"
try:
self.cursor.execute(requete)
print('insertion succed')
self.cnx.commit()
except :
print('insertion error')
def delete_IntoTable(self,table, id_table) :
pre_requete = "SELECT COUNT(id) FROM {} WHERE id = {};".format(table , id_table)
try :
self.cursor.execute(pre_requete)
val = self.cursor.fetchall()
if val[0][0] == 1:
requete = "DELETE FROM {} WHERE id = {} ".format(table , id_table)
self.cursor.execute(requete)
print("delete item succed")
self.cnx.commit()
else:
print ("L'item que vous essayez de supprimer n'existe pas")
except:
print("Impossible de se connecter a la bdd")
def delete_Table(self , table):
requete = "DROP TABLE IF EXISTS {}".format(table)
try :
print("!Deleting: {}".format(table))
self.cursor.execute(requete)
print("delete table succed")
self.cnx.commit()
except :
print ("delete table error")
def getDBInfo(tabl = tables):
liste_tables = []
for name in tables.keys():
liste_tables.append(name)
return liste_tables
def execSqlcommand(self):
requete = input("veuillez saisir votre SQL commande : \n")
try:
self.cursor.execute(requete)
print("requete executée")
val = self.cursor.fetchall()
print(val)
self.cnx.commit()
except :
print("syntax invalid")
def sommeMood(self , date1 ,date2):
requete = """select count(*) from Mood as m
WHERE STR_TO_DATE( m.dateSTR , '%Y/%m/%d %T') BETWEEN '{}' AND '{}'
;""".format(date1, date2)
try:
self.cursor.execute(requete)
somme =(self.cursor.fetchall())
except mysql.connector.Error as err:
print("Failed retrieving database: {}".format(err))
return somme[0][0]
def agregationMood(self, date1, date2):
agr = {}
requete = """select humeur ,count(*) as nb from Mood as m
WHERE STR_TO_DATE( m.dateSTR , '%Y/%m/%d %T') BETWEEN '{}' AND '{}'
group by m.humeur;""".format(date1, date2)
print(requete)
try:
self.cursor.execute(requete)
agregation =(self.cursor.fetchall())
except mysql.connector.Error as err:
print("Failed retrieving database: {}".format(err))
for i in agregation:
agr[i[0]] = i[1]
return agr
def getMood(self):
requete = """select * from Mood order by STR_TO_DATE( Mood.dateSTR , '%Y/%m/%d %T') desc;"""
try:
self.cursor.execute(requete)
mood =(self.cursor.fetchall())
except mysql.connector.Error as err:
print("Failed retrieving database: {}".format(err))
return(mood[:6])
def create_table(self,
tables,
http = dbURL ,
base = schName,
user = userName,
password = pswDB,
isNew = False):
for name, ddl in tables.items():
if isNew :
command = "DROP TABLE IF EXISTS " + name
try :
print ("Dropping table {} =>".format(name))
self.cursor.execute(command)
except:
print (" error during drop ?")
raise
else:
print (" Drop ok")
try:
print("Creating table {}: ".format(name))
self.cursor.execute(ddl)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("already exists.")
else:
print(err.msg)
raise
else:
print("OK")
continue
return
#import a table from a csv file
#will create a table adapted to the file
#if table name already exists, will be deleted
def importTable(self, csvFile, delimiter, colTypes):
tableName = csvFile.split(".")[-2].split("\\")[-1]
if csvFile.split("\\")[0] not in ['C:', 'H:', 'D:']:
csvFile = mysqlStringPP(CWD + '\\' + csvFile)
print(csvFile)
#Delete existing tablez
self.delete_Table(tableName)
"""
command = "DROP TABLE IF EXISTS " + tableName
try :
print ("Dropping table {} =>".format(tableName))
self.cursor.execute(command)
except:
print (" error during drop ?")
raise
else:
print (" Drop ok")
"""
#create sql table creation statement
ddl = "CREATE TABLE `{}` (""`ID` bigint (32) AUTO_INCREMENT,".format(tableName)
for col, typ in colTypes.items():
ddl += " `{}` {} NOT NULL,".format(col, typ)
ddl += " PRIMARY KEY (`ID`)"") ENGINE=InnoDB"
#execute it
try:
print("Creating table {}: ".format(tableName))
self.cursor.execute(ddl)
self.cnx.commit()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("already exists.")
else:
print(err.msg)
raise
else:
print("OK")
#finally load the file
headers = 1
#LINES TERMINATED BY '\\r \\n' IGNORE
ddl = "LOAD DATA LOCAL INFILE '{}' INTO TABLE {} FIELDS TERMINATED BY '{}' IGNORE {} ROWS (".format(csvFile, tableName, delimiter, headers)
for col in colTypes.keys():
ddl+= "{}.{} ,".format(tableName, col)
ddl = ddl[:-1]
ddl+= ") SET ID = NULL ;"
print(ddl)
try:
print("import file: {}".format(csvFile))
self.cursor.execute(ddl)
self.cnx.commit()
except mysql.connector.Error as err:
print(err.msg)
raise
else:
print("ok")
return
def mysqlStringPP(text):
import re
# define desired replacements here
rep = {
"'":"''",
'"':'""',
"\n":"\n",
'\{}'.format(''):"\\\\"
}
# use these three lines to do the replacement
rep = dict((re.escape(k), v) for k, v in rep.items())
pattern = re.compile("|".join(rep.keys()))
return pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
def menu():
print("\nGestion de la base de données:\n")
print("0: quitter\n1: ajouter un élement dans la base\n2: supprimer un élement de la base\n3: afficher la base\n4: input a direct SQL command (check your syntax!!)\n5: creer ou maj la base de données\n6: importer une table depuis un .csv")
Base = monSql()
choix = input("choix: ")
print("\n")
if choix == "1" :
liste_table = Base.getDBInfo()
for table in liste_table :
print(table)
table = ''
while table not in liste_table:
table = input("Dans quelle table voulez vous faire une insertion ?\n")
res = []
fields = Base.getFieldTable(table)
print(fields)
for col in fields :
if col[1] != 'id' :
res.append(input("{} :".format(col)))
Base.insert_IntoTable(table , fields , res)
elif choix == "2" :
liste_table = Base.getDBInfo()
for table in liste_table :
print(table)
choixTable = ''
while choixTable not in liste_table:
choixTable = input("De quelle table s'agit-il ? \n")
print('[' + choixTable + ']')
listeItem = Base.getItemIntoTable(choixTable)
for items in listeItem :
print(items)
choixId = input("Quelle id voulez vous supprimer \n")
Base.delete_IntoTable(choixTable, choixId)
elif choix == "3":
liste_table = Base.getDBInfo()
for table in liste_table:
print('[' + table + ']')
listeItem = Base.getItemIntoTable(table)
for items in listeItem :
print(items)
print('\n')
elif choix == "4":
Base.execSqlcommand()
elif choix == "5" :
Base.create_table(tables)
elif choix == "6":
try:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
except:
filename = input("entrer le nom du fichier avec son chemin\n")
pass
delimiter = input("delimiter?\n")
with open(filename, 'r') as file:
reader = csv.reader(file, delimiter = delimiter)
cols = next(reader)
colTypes = {}
for col in cols:
colName = input("Nom de la colonne: (sugg: {})\n".format(col))
colTypes[colName] = None
while colTypes[colName] not in SQLtypes:
colTypes[colName] = input("type de la columne parmi {}\n".format(SQLtypes))
Base.importTable(filename, delimiter, colTypes)
elif choix == "0":
Base.close()
return 1
else :
menu()
menu()
def getDates(delta):
d = datetime.datetime.now()
d1=d.strftime('%Y/%m/%d')
date1 = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
if delta==1:
date2=str(d1 + " " + "00:00:00")
else:
d2=d- datetime.timedelta(days=delta)
date2=d2.strftime('%Y/%m/%d %H:%M:%S')
return date1, date2
if __name__ == "__main__" :
print("Not this file to execute please try serverSlack.py")