From edf405c5801bdfb4d8193e1cb1d5483ecf2ff42d Mon Sep 17 00:00:00 2001 From: cartoonnerie Date: Wed, 7 Feb 2018 13:05:47 +0100 Subject: [PATCH 1/2] j'ai mis des try finally partout --- cineclubBlogSQ.py | 122 ++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 58 deletions(-) diff --git a/cineclubBlogSQ.py b/cineclubBlogSQ.py index f597946..85d42e6 100644 --- a/cineclubBlogSQ.py +++ b/cineclubBlogSQ.py @@ -26,60 +26,64 @@ class filmSQ(): self.idN = dic['idN'] - conn = sqlite3.connect("baseFilms.db") - conn.row_factory = sqlite3.Row - c = conn.cursor() + try: + conn = sqlite3.connect("baseFilms.db") + 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']] - c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq) - - conn.close() + acteursToSq = [{'idFilm' : self.i, 'acteur' : a} for a in dic['acteurs']] + c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq) + finally: + conn.close() #id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT [PRIMARY KEY] def __setattr__(self, nom_attr, val_attr): - conn = sqlite3.connect("baseFilms.db") - conn.row_factory = sqlite3.Row - c = conn.cursor() - - if nom_attr == 'acteurs': - c.execute('DELETE FROM acteurs WHERE idFilm =?', (self.i)) - acteursToSq = [{'idFilm' : self.i, 'acteur' : a} for a in val_attr] - c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq) - else: - c.execute('UPDATE films SET ? = ? WHERE idN = ?', (nom_attr, val_attr, self.idN)) - - conn.close() + try: + conn = sqlite3.connect("baseFilms.db") + conn.row_factory = sqlite3.Row + c = conn.cursor() + + if nom_attr == 'acteurs': + c.execute('DELETE FROM acteurs WHERE idFilm =?', (self.i)) + acteursToSq = [{'idFilm' : self.i, 'acteur' : a} for a in val_attr] + c.executemany('INSERT INTO acteurs VALUES (NULL, :idFilm, :acteur)', acteursToSq) + else: + c.execute('UPDATE films SET ? = ? WHERE idN = ?', (nom_attr, val_attr, self.idN)) + finally: + conn.close() def __getattr__(self, nom): - conn = sqlite3.connect("baseFilms.db") - conn.row_factory = sqlite3.Row - c = conn.cursor() - if nom == 'acteurs': - c.execute('SELECT :acteur FROM acteurs WHERE idFilms=?', (self.i)) - r = c.fetchall() - res = [a[0] for a in r] - else: - c.execute('SELECT :nom_attr FROM films WHERE idN=:idN', {"idN":self.idN, "nom_attr":nom}) - r = c.fetchone() - res = r[0] - - conn.close() + try: + conn = sqlite3.connect("baseFilms.db") + conn.row_factory = sqlite3.Row + c = conn.cursor() + if nom == 'acteurs': + c.execute('SELECT :acteur FROM acteurs WHERE idFilms=?', (self.i)) + r = c.fetchall() + res = [a[0] for a in r] + else: + c.execute('SELECT :nom_attr FROM films WHERE idN=:idN', {"idN":self.idN, "nom_attr":nom}) + r = c.fetchone() + res = r[0] + finally: + conn.close() return res def __str__(self): - conn = sqlite3.connect("baseFilms.db") + try: + conn = sqlite3.connect("baseFilms.db") - conn.row_factory = sqlite3.Row - c = conn.cursor() - c.execute('SELECT (?,?,?) FROM films WHERE idN = ?', ('idN', 'nom', 'date', self.idN)) - r = c.fetchone() - s = r[0] + ' : ' + r[1] + ' le ' + r[2] - - conn.close() + conn.row_factory = sqlite3.Row + c = conn.cursor() + c.execute('SELECT (?,?,?) FROM films WHERE idN = ?', ('idN', 'nom', 'date', self.idN)) + r = c.fetchone() + s = r[0] + ' : ' + r[1] + ' le ' + r[2] + finally: + conn.close() return s @@ -184,28 +188,30 @@ class filmSQ(): return res def maxId(): - conn = sqlite3.connect("baseFilms.db") + try: + conn = sqlite3.connect("baseFilms.db") - conn.row_factory = sqlite3.Row - c = conn.cursor() - c.execute('SELECT MAX(i) FROM films') - r = c.fetchone() - - conn.close() + conn.row_factory = sqlite3.Row + c = conn.cursor() + c.execute('SELECT MAX(i) FROM films') + r = c.fetchone() + finally: + conn.close() return r[0] def printAll(): - conn = sqlite3.connect("baseFilms.db") - - conn.row_factory = sqlite3.Row - c = conn.cursor() - c.execute('SELECT idN FROM films ORDER_BY i') - r = c.fetchall() - for movie in r: - print(r[0] + ' : ' + r[1] + ' le ' + r[2]) - - conn.close() + try: + conn = sqlite3.connect("baseFilms.db") + + conn.row_factory = sqlite3.Row + c = conn.cursor() + c.execute('SELECT idN FROM films ORDER_BY i') + r = c.fetchall() + for movie in r: + print(r[0] + ' : ' + r[1] + ' le ' + r[2]) + finally: + conn.close() pass def strListe(liste): From 21d100830fcd23754c7ee10ff387b709d5ec0754 Mon Sep 17 00:00:00 2001 From: cartoonnerie Date: Wed, 7 Feb 2018 13:14:01 +0100 Subject: [PATCH 2/2] ajout todo --- TODO.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 TODO.md diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..c5cf9c8 --- /dev/null +++ b/TODO.md @@ -0,0 +1,3 @@ +* mise en forme date +* débug +* remplissage