sql command file load

This commit is contained in:
katboi01 2025-01-13 01:14:58 +01:00
parent 50559223ed
commit 1489354ed6
2 changed files with 24 additions and 3 deletions

View File

@ -6,7 +6,7 @@ TABLE_X = "x_posts"
class DatabaseController: class DatabaseController:
def __init__(self, db_name): def __init__(self, db_name):
self.conn = sqlite3.connect(db_name, isolation_level="DEFERRED") self.conn = sqlite3.connect(db_name)
self.cursor = self.conn.cursor() self.cursor = self.conn.cursor()
def run_query(self, query): def run_query(self, query):
@ -18,15 +18,28 @@ class DatabaseController:
print(e) print(e)
return None return None
def run_command(self, commnd): def run_command(self, command):
try: try:
self.cursor.execute(commnd) self.cursor.execute(command)
result = self.cursor.rowcount result = self.cursor.rowcount
self.conn.commit() self.conn.commit()
return result > 0 return result > 0
except Exception as e: except Exception as e:
self.conn.rollback()
print(e) print(e)
return False return False
def run_commands(self, commands):
try:
for command in commands:
self.cursor.execute(command)
result = self.cursor.rowcount
self.conn.commit()
return result > 0
except Exception as e:
self.conn.rollback()
print(e)
return False
def close(self): def close(self):
self.conn.close() self.conn.close()

View File

@ -0,0 +1,8 @@
import sys
from .databaseController import DatabaseController
db = DatabaseController("/home/pi/python/Katbots/JapariArchive/database.db")
with open(sys.argv[1], "rt") as file:
lines = [line for line in file.readlines() if line.strip()]
print(db.run_commands(lines))