FMN_bot/src/fmn_states_db.py

42 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sqlite3
from loguru import logger
conn = sqlite3.connect("fmn_states.sqlite", check_same_thread=False)
c = conn.cursor()
c.execute(
'''CREATE TABLE IF NOT EXISTS states(key VARCHAR (500) UNIQUE DEFAULT NULL, value VARCHAR (500) UNIQUE DEFAULT NULL)''')
conn.commit()
def add_state(key, value):
'''Создание стейта, если существует - будет заменен'''
logger.debug(f'Adding {key}: {value}')
c.execute("INSERT OR REPLACE INTO states(key, value) VALUES (?, ?)", (key, value))
conn.commit()
logger.warning(f'Добавлен стейт {key}: {value}')
def get_state(key):
'''Получение стейта по ключу'''
logger.trace(f'Запрошен стейт {key}')
value = c.execute("SELECT value FROM states WHERE key = (?)", (key,)).fetchone()
if value:
return value[0]
def remove_state(key):
'''Удалить стейт по ключу'''
c.execute("DELETE FROM states WHERE key = (?)", (key,))
conn.commit()
logger.warning(f'Удален стейт {key}')
def clear_all_states():
c.execute("DELETE FROM states")
conn.commit()
logger.warning(f'Все стейты удалены')