import sqlite3, datetime class SQLite(): def __init__(self, file='sqlite.db'): self.file=file def __enter__(self): self.conn = sqlite3.connect(self.file) self.conn.row_factory = sqlite3.Row return self.conn.cursor() def __exit__(self, type, value, traceback): self.conn.commit() self.conn.close() def create_status_table(): with SQLite() as cur: cur.execute(""" CREATE TABLE `status` ( `ID` INTEGER PRIMARY KEY AUTOINCREMENT, `drive` TEXT, `date` NUMERIC, `type` TEXT, `fast` INTEGER, `delayed` INTEGER, `re` INTEGER, `total_e_cottect` INTEGER, `correction` INTEGER, `proc_gb` INTEGER, `total_u_errors` INTEGER, `Non-medium error count` INTEGER ); """) def table_exist(name): with SQLite() as cur: cur.execute(''' SELECT name FROM sqlite_master WHERE type='table' AND name='{}'; '''.format(name)) return True if cur.fetchone() else False def add_status(drive, type, status, NM_count): now = int(datetime.datetime.now().timestamp()) with SQLite() as cur: cur.execute( ''' INSERT INTO status (drive, date, type, fast, delayed, re, total_e_cottect, correction, proc_gb, total_u_errors, "Non-medium error count") VALUES (?,?,?,?,?,?,?,?,?,?,?) ''',(drive, now, type, *status, NM_count) ) def get_status(drive): with SQLite() as cur: cur.execute(''' SELECT * FROM status WHERE drive=(?) ''',(drive,)) return cur.fetchall() if __name__ == '__main__': if not table_exist('status'): print("Creating table") create_status_table() else: print('Table table exist!') test = {'read': ['314277204', '9', '0', '314277213', '9', '229503.982', '0'], 'write': ['0', '0', '0', '0', '0', '47762.811', '0'], 'verify': ['64', '0', '0', '64', '0', '0.000', '0'], 'Non-medium error count': '10'}