Files
qwork/database.py
2026-03-28 21:39:27 +03:00

47 lines
1.7 KiB
Python

import aiosqlite
class Database:
def __init__(self, db_path: str):
self.db_path = db_path
async def create_tables(self):
async with aiosqlite.connect(self.db_path) as db:
await db.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
user_id INTEGER UNIQUE,
sphere TEXT,
language TEXT,
preferences TEXT
)
""")
await db.commit()
async def add_user(self, user_id: int):
async with aiosqlite.connect(self.db_path) as db:
await db.execute(
"INSERT OR IGNORE INTO users (user_id) VALUES (?)",
(user_id,)
)
await db.commit()
async def update_user_data(self, user_id: int, data: dict):
async with aiosqlite.connect(self.db_path) as db:
await db.execute(
"UPDATE users SET sphere = ?, language = ?, preferences = ? WHERE user_id = ?",
(data.get('sphere'), data.get('language'), data.get('preferences'), user_id)
)
await db.commit()
async def get_all(self):
async with aiosqlite.connect(self.db_path) as db:
async with db.execute("SELECT * FROM users") as cursor:
return await cursor.fetchall()
async def get_user(self, user_id: int):
async with aiosqlite.connect(self.db_path) as db:
async with db.execute(
"SELECT sphere, language, preferences FROM users WHERE user_id = ?",
(user_id,)
) as cursor:
return await cursor.fetchone()