fsfwgen/utility/sql_writer.py

37 lines
1.1 KiB
Python
Raw Normal View History

2021-06-08 12:37:10 +02:00
import sqlite3
class SqlWriter:
def __init__(self, db_filename: str):
self.filename = db_filename
self.conn = sqlite3.connect(self.filename)
def open(self, sql_creation_command: str):
print("SQL Writer: Opening " + self.filename)
self.conn.execute(sql_creation_command)
def delete(self, sql_deletion_command):
print("SQL Writer: Deleting SQL table")
self.conn.execute(sql_deletion_command)
def write_entries(self, sql_insertion_command, current_entry):
cur = self.conn.cursor()
cur.execute(sql_insertion_command, current_entry)
return cur.lastrowid
def commit(self):
print("SQL Writer: Commiting SQL table")
self.conn.commit()
def close(self):
self.conn.close()
def sql_writing_helper(self, creation_cmd, insertion_cmd, mib_table: dict, deletion_cmd: str=""):
if deletion_cmd != "":
self.delete(deletion_cmd)
self.open(creation_cmd)
for i in mib_table:
self.write_entries(insertion_cmd, mib_table[i])
self.commit()
self.close()