mirror of
https://gitea.phreedom.club/localhost_frssoft/FMN_bot.git
synced 2024-09-28 21:47:50 +00:00
44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import sqlite3
|
||
import logging
|
||
|
||
|
||
logger = logging.getLogger('fmn_states_database')
|
||
|
||
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.debug(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'Все стейты удалены')
|
||
|