From fb26b02cd28522b3f3b1266482bcb52f14b35041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=92=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Sat, 12 Mar 2016 19:22:33 +0300 Subject: [PATCH] upd history --- src/history.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/history.py b/src/history.py index b04c918..4bb28e8 100644 --- a/src/history.py +++ b/src/history.py @@ -35,9 +35,9 @@ class History(object): ' message_type INTEGER' ')') db.commit() - except Exception as e: + except: db.rollback() - raise e + raise finally: db.close() @@ -49,12 +49,21 @@ class History(object): cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, )) cursor.execute('DROP TABLE id' + tox_id + ';') db.commit() - except Exception as e: + except: db.rollback() - raise e + raise finally: db.close() + def friend_exists_in_db(self, tox_id): + os.chdir(Settings.get_default_path()) + db = sqlite3.connect(self._name + '.hstr') + cursor = db.cursor() + cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, )) + result = cursor.fetchone() + db.close() + return result is not None + def save_messages_to_db(self, tox_id, messages_iter): os.chdir(Settings.get_default_path()) db = sqlite3.connect(self._name + '.hstr') @@ -63,9 +72,9 @@ class History(object): cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time) ' 'VALUES (?, ?, ?);', messages_iter) db.commit() - except Exception as e: + except: db.rollback() - raise e + raise finally: db.close() @@ -96,5 +105,4 @@ class History(object): if __name__ == '__main__': h = History('test') getter = h.messages_getter('42') - print getter.get_all() - print getter.get(5) + print h.friend_exists_in_db('42'), type(h.friend_exists_in_db('42'))