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 diff --git a/cineclubBlogSQ.py b/cineclubBlogSQ.py index f49d45a..0cec4d3 100644 --- a/cineclubBlogSQ.py +++ b/cineclubBlogSQ.py @@ -46,25 +46,24 @@ class filmSQ(): if nom_attr == 'idN': res= object.__setattr__(self, 'idN', val_attr) else: - 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.commit() - c.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: + c.commit() + conn.close() def __getattr__(self, nom): - - if nom == 'idN': - res= object.__getattr__(self, idN) - else: + try: conn = sqlite3.connect("baseFilms.db") conn.row_factory = sqlite3.Row c = conn.cursor() @@ -76,22 +75,23 @@ class filmSQ(): c.execute('SELECT :nom_attr FROM films WHERE idN=:idN', {"idN":self.idN, "nom_attr":nom}) r = c.fetchone() res = r[0] - + + finally: + c.commit() conn.close() return res def __str__(self): - 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.commit() - c.close() + 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] + finally: + conn.close() return s @@ -196,29 +196,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.commit() - c.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):