-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
executable file
·179 lines (162 loc) · 5.46 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
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
import MySQLdb
import time
db = MySQLdb.connect("localhost", "root", "123456", "bd_notes")
cursor = db.cursor()
global resultsExportEtudiants
resultsExportEtudiants = []
global resultsExportMatieres
resultsExportMatieres = []
global resultsExportNotes
resultsExportNotes = []
global resultsExportNotesEtu
resultsExportNotesEtu = []
def getmatieres():
del resultsExportMatieres[:]
sql = "SELECT * FROM t_matiere"
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
item = {
"id": row[0],
"libelle": row[1],
"coefficient": row[2]
}
resultsExportMatieres.append(item)
except MySQLdb.Error as e:
try:
print ("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
return None
except IndexError:
print ("MySQL Error: %s" % str(e))
return None
finally:
cursor.close()
db.close()
def getetudiants():
del resultsExportEtudiants[:]
sql = "SELECT * FROM t_etudiant"
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
item = {
"id_etudiant": row[0],
"matricule": row[1],
"prenom": row[2],
"nom": row[3]
}
resultsExportEtudiants.append(item)
except MySQLdb.Error as e:
try:
print ("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
return None
except IndexError:
print ("MySQL Error: %s" % str(e))
return None
finally:
cursor.close()
db.close()
def getnotes():
del resultsExportNotes[:]
sql = "SELECT t_note.valeur as note, t_matiere.libelle as lib, t_matiere.coefficient as coef, t_etudiant.nom as nom, t_etudiant.prenom as pren, t_etudiant.matricule as mat FROM t_note, t_matiere, t_etudiant WHERE t_note.id_etudiant = t_etudiant.id_etudiant AND t_note.id_matiere = t_matiere.id_matire"
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
item = {
"note": row[0],
"lib": row[1],
"coef": row[2],
"nom": row[3],
"pren": row[4],
"mat": row[5],
}
resultsExportNotes.append(item)
except MySQLdb.Error as e:
try:
print ("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
return None
except IndexError:
print ("MySQL Error: %s" % str(e))
return None
finally:
cursor.close()
db.close()
def getnotesetu(id_etu):
del resultsExportNotesEtu[:]
sql = "SELECT t_note.valeur as note, t_matiere.libelle as lib, t_matiere.coefficient as coef, t_etudiant.nom as nom, t_etudiant.prenom as pren, t_etudiant.matricule as mat FROM t_note, t_matiere, t_etudiant " \
"WHERE t_note.id_etudiant = t_etudiant.id_etudiant AND t_note.id_matiere = t_matiere.id_matire AND t_etudiant.id_etudiant = '%d'" \
% id_etu
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
item = {
"note": row[0],
"lib": row[1],
"coef": row[2],
"nom": row[3],
"pren": row[4],
"mat": row[5],
}
resultsExportNotesEtu.append(item)
except MySQLdb.Error as e:
try:
print ("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
return None
except IndexError:
print ("MySQL Error: %s" % str(e))
return None
finally:
cursor.close()
db.close()
def creatematiere(matiere):
sql = "Insert into t_matiere(libelle, coefficient) values('%s', '%d')" % (matiere['libelle'], matiere['coefficient'])
try:
cursor.execute(sql)
db.commit()
except MySQLdb.Error as e:
try:
print ("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
return None
except IndexError:
db.rollback()
print ("MySQL Error: %s" % str(e))
return None
finally:
cursor.close()
db.close()
def createetudiant(etudiant):
sql = "Insert into t_etudiant(matricule, nom, prenom) values('%s', '%s', '%s')" % (etudiant['matricule'], etudiant['nom'], etudiant['prenom'])
try:
cursor.execute(sql)
db.commit()
except MySQLdb.Error as e:
try:
print ("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
return None
except IndexError:
db.rollback()
print ("MySQL Error: %s" % str(e))
return None
finally:
cursor.close()
db.close()
def createnote(note):
now = time.strftime('%Y-%m-%d %H:%M:%S')
sql = "Insert into t_note(valeur, date_creation, id_matiere, id_etudiant) values('%d', '%s', '%d', '%d')" % (note['valeur'], now, note['id_matiere'], note['id_etudiant'])
try:
cursor.execute(sql)
db.commit()
except MySQLdb.Error as e:
try:
print ("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
return None
except IndexError:
db.rollback()
print ("MySQL Error: %s" % str(e))
return None
finally:
cursor.close()
db.close()