borg-manager/Database.py
George Lacey f6bd684b97 Add insert method
Method inserts log entry to database
2016-09-20 20:55:25 +01:00

48 lines
1.5 KiB
Python

import sqlite3
class Database(object):
def __init__(self, path):
self.conn = sqlite3.connect(path)
def __del__(self):
self.conn.close()
def create_table(self, name, row_list):
if self.table_exists(name):
return True
else:
row_string = ""
separator = ", "
for i in range(0, len(row_list)):
if i == len(row_list) - 1:
separator = ""
row_string += "%s%s" % (row_list[i], separator)
self.conn.execute("CREATE TABLE %s(%s)" % (name, row_string))
if self.table_exists(name):
return True
else:
return False
def table_exists(self, name):
result = self.conn.execute("""SELECT * FROM sqlite_master
WHERE type='table' AND name=?""", (name,))
if result.fetchone() is None:
return False
else:
return True
def insert(self, log_entry, table):
result = self.conn.execute("""INSERT INTO %s(NAME,
FINGERPRINT, START_TIME, DURATION, FILE_COUNT) VALUES(?,?,?,?,?)"""
% table,
(log_entry.name,
log_entry.fingerprint,
log_entry.datetime_string(),
"1",
log_entry.file_count))
self.conn.commit()