You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

import aiosqlite
from enum import Enum, auto
class DBAction(Enum):
fetchone = auto()
fetchall = auto()
commit = auto()
async def db_action(sql: str, args: tuple, action: DBAction):
"""Runs a query on the database"""
conn = await aiosqlite.connect('db.sqlite')
cursor = await conn.cursor()
await cursor.execute(sql, args)
if action == DBAction.fetchone:
result = await cursor.fetchone()
elif action == DBAction.fetchall:
result = await cursor.fetchall()
elif action == DBAction.commit:
await conn.commit()
result = cursor.lastrowid
await cursor.close()
await conn.close()
return result
async def create_table():
"""Creates a table in the database"""
conn = await aiosqlite.connect('db.sqlite')
cursor = await conn.cursor()
await cursor.execute("""CREATE TABLE IF NOT EXISTS user_exceptions (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
chat_id INTEGER NOT NULL
)""")
await cursor.execute("""CREATE TABLE IF NOT EXISTS banned_stickerpacks (
id INTEGER PRIMARY KEY,
pack_id INTEGER NOT NULL,
chat_id INTEGER NOT NULL
)""")
await cursor.close()
await conn.close()