j'ai mis des try finally partout

This commit is contained in:
Alice 2018-02-07 13:05:47 +01:00
parent b411dc85e2
commit edf405c580

View file

@ -26,60 +26,64 @@ class filmSQ():
self.idN = dic['idN'] self.idN = dic['idN']
conn = sqlite3.connect("baseFilms.db") try:
conn.row_factory = sqlite3.Row conn = sqlite3.connect("baseFilms.db")
c = conn.cursor() conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('INSERT INTO films VALUES (:i, :idN, :date, :nom, realisateur, :duree, :synopsis, :pays, :annee, :youtube, :couleur, :image, :formatCopie, :langST)', dic) c.execute('INSERT INTO films VALUES (:i, :idN, :date, :nom, realisateur, :duree, :synopsis, :pays, :annee, :youtube, :couleur, :image, :formatCopie, :langST)', dic)
acteursToSq = [{'idFilm' : self.i, 'acteur' : a} for a in dic['acteurs']] acteursToSq = [{'idFilm' : self.i, 'acteur' : a} for a in dic['acteurs']]
c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq) c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq)
finally:
conn.close() conn.close()
#id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT [PRIMARY KEY] #id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT [PRIMARY KEY]
def __setattr__(self, nom_attr, val_attr): def __setattr__(self, nom_attr, val_attr):
conn = sqlite3.connect("baseFilms.db") try:
conn.row_factory = sqlite3.Row conn = sqlite3.connect("baseFilms.db")
c = conn.cursor() conn.row_factory = sqlite3.Row
c = conn.cursor()
if nom_attr == 'acteurs':
c.execute('DELETE FROM acteurs WHERE idFilm =?', (self.i)) if nom_attr == 'acteurs':
acteursToSq = [{'idFilm' : self.i, 'acteur' : a} for a in val_attr] c.execute('DELETE FROM acteurs WHERE idFilm =?', (self.i))
c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq) acteursToSq = [{'idFilm' : self.i, 'acteur' : a} for a in val_attr]
else: c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq)
c.execute('UPDATE films SET ? = ? WHERE idN = ?', (nom_attr, val_attr, self.idN)) else:
c.execute('UPDATE films SET ? = ? WHERE idN = ?', (nom_attr, val_attr, self.idN))
conn.close() finally:
conn.close()
def __getattr__(self, nom): def __getattr__(self, nom):
conn = sqlite3.connect("baseFilms.db") try:
conn.row_factory = sqlite3.Row conn = sqlite3.connect("baseFilms.db")
c = conn.cursor() conn.row_factory = sqlite3.Row
if nom == 'acteurs': c = conn.cursor()
c.execute('SELECT :acteur FROM acteurs WHERE idFilms=?', (self.i)) if nom == 'acteurs':
r = c.fetchall() c.execute('SELECT :acteur FROM acteurs WHERE idFilms=?', (self.i))
res = [a[0] for a in r] r = c.fetchall()
else: res = [a[0] for a in r]
c.execute('SELECT :nom_attr FROM films WHERE idN=:idN', {"idN":self.idN, "nom_attr":nom}) else:
r = c.fetchone() c.execute('SELECT :nom_attr FROM films WHERE idN=:idN', {"idN":self.idN, "nom_attr":nom})
res = r[0] r = c.fetchone()
res = r[0]
conn.close() finally:
conn.close()
return res return res
def __str__(self): def __str__(self):
conn = sqlite3.connect("baseFilms.db") try:
conn = sqlite3.connect("baseFilms.db")
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
c = conn.cursor() c = conn.cursor()
c.execute('SELECT (?,?,?) FROM films WHERE idN = ?', ('idN', 'nom', 'date', self.idN)) c.execute('SELECT (?,?,?) FROM films WHERE idN = ?', ('idN', 'nom', 'date', self.idN))
r = c.fetchone() r = c.fetchone()
s = r[0] + ' : ' + r[1] + ' le ' + r[2] s = r[0] + ' : ' + r[1] + ' le ' + r[2]
finally:
conn.close() conn.close()
return s return s
@ -184,28 +188,30 @@ class filmSQ():
return res return res
def maxId(): def maxId():
conn = sqlite3.connect("baseFilms.db") try:
conn = sqlite3.connect("baseFilms.db")
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
c = conn.cursor() c = conn.cursor()
c.execute('SELECT MAX(i) FROM films') c.execute('SELECT MAX(i) FROM films')
r = c.fetchone() r = c.fetchone()
finally:
conn.close() conn.close()
return r[0] return r[0]
def printAll(): def printAll():
conn = sqlite3.connect("baseFilms.db") try:
conn = sqlite3.connect("baseFilms.db")
conn.row_factory = sqlite3.Row
c = conn.cursor() conn.row_factory = sqlite3.Row
c.execute('SELECT idN FROM films ORDER_BY i') c = conn.cursor()
r = c.fetchall() c.execute('SELECT idN FROM films ORDER_BY i')
for movie in r: r = c.fetchall()
print(r[0] + ' : ' + r[1] + ' le ' + r[2]) for movie in r:
print(r[0] + ' : ' + r[1] + ' le ' + r[2])
conn.close() finally:
conn.close()
pass pass
def strListe(liste): def strListe(liste):