KemoFureApi/modules/Archive/databaseController.py

48 lines
1.3 KiB
Python

import os
import sqlite3
TABLE_ACCOUNTS = "accounts"
TABLE_X = "x_posts"
class DatabaseController:
def __init__(self, db_name):
self.conn = sqlite3.connect(db_name)
self.conn.row_factory = sqlite3.Row
self.cursor = self.conn.cursor()
def run_query(self, query):
try:
self.cursor.execute(query)
results = [dict(result) for result in self.cursor.fetchall()]
return results
except Exception as e:
print(e)
return []
def run_command(self, command):
try:
self.cursor.execute(command)
result = self.cursor.rowcount
self.conn.commit()
return result > 0
except Exception as e:
self.conn.rollback()
print(command, e)
return False
def run_commands(self, commands):
last_command = ""
try:
for command in commands:
last_command = command
self.cursor.execute(command)
result = self.cursor.rowcount
self.conn.commit()
return result > 0
except Exception as e:
self.conn.rollback()
print(last_command, e)
return False
def close(self):
self.conn.close()