From f6a52ece4dc67456efe881fa3e0b02687ab82401 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 21 Sep 2016 20:33:44 +0100 Subject: [PATCH] Add query() - Add query() to query db and return result as list of attributes - Refactor row_string -> rows in create_table() - Add method documentation --- Database.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Database.py b/Database.py index 49ba7ea..9e8364b 100644 --- a/Database.py +++ b/Database.py @@ -10,24 +10,28 @@ class Database(object): self.conn.close() def create_table(self, name, row_list): + """creates table 'name' with rows from 'row_list' if it doesn't exist""" + if self.table_exists(name): return True else: - row_string = "" + rows = "" separator = ", " for i in range(0, len(row_list)): if i == len(row_list) - 1: separator = "" - row_string += "%s%s" % (row_list[i], separator) + rows += "%s%s" % (row_list[i], separator) - self.conn.execute("CREATE TABLE %s(%s)" % (name, row_string)) + self.conn.execute("CREATE TABLE %s(%s)" % (name, rows)) if self.table_exists(name): return True else: return False def table_exists(self, name): + """returns true if table 'name' exists""" + result = self.conn.execute("""SELECT * FROM sqlite_master WHERE type='table' AND name=?""", (name,)) if result.fetchone() is None: @@ -45,3 +49,6 @@ class Database(object): log_entry.duration, log_entry.file_count)) self.conn.commit() + + def query(self, query): + return self.conn.execute(query).fetchall()