2024-12-26 22:54:55 +08:00
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
TABLE_ACCOUNTS = "accounts"
|
|
|
|
TABLE_X = "x_posts"
|
|
|
|
|
|
|
|
class DatabaseController:
|
|
|
|
def __init__(self, db_name):
|
2025-01-13 08:14:58 +08:00
|
|
|
self.conn = sqlite3.connect(db_name)
|
2025-01-16 04:28:49 +08:00
|
|
|
self.conn.row_factory = sqlite3.Row
|
2024-12-26 22:54:55 +08:00
|
|
|
self.cursor = self.conn.cursor()
|
|
|
|
|
|
|
|
def run_query(self, query):
|
|
|
|
try:
|
|
|
|
self.cursor.execute(query)
|
2025-01-16 04:28:49 +08:00
|
|
|
results = [dict(result) for result in self.cursor.fetchall()]
|
2024-12-26 22:54:55 +08:00
|
|
|
return results
|
2024-12-30 16:35:57 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2025-01-16 04:28:49 +08:00
|
|
|
return []
|
2025-01-02 19:05:25 +08:00
|
|
|
|
2025-01-13 08:14:58 +08:00
|
|
|
def run_command(self, command):
|
2025-01-02 19:05:25 +08:00
|
|
|
try:
|
2025-01-13 08:14:58 +08:00
|
|
|
self.cursor.execute(command)
|
2025-01-02 19:05:25 +08:00
|
|
|
result = self.cursor.rowcount
|
|
|
|
self.conn.commit()
|
|
|
|
return result > 0
|
|
|
|
except Exception as e:
|
2025-01-13 08:14:58 +08:00
|
|
|
self.conn.rollback()
|
2025-01-02 19:05:25 +08:00
|
|
|
print(e)
|
|
|
|
return False
|
2024-12-26 22:54:55 +08:00
|
|
|
|
2025-01-13 08:14:58 +08:00
|
|
|
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
|
|
|
|
|
2024-12-26 22:54:55 +08:00
|
|
|
def close(self):
|
|
|
|
self.conn.close()
|