Add query()
- Add query() to query db and return result as list of attributes - Refactor row_string -> rows in create_table() - Add method documentation
This commit is contained in:
parent
3229d125b3
commit
f6a52ece4d
13
Database.py
13
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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user