States storage as sqlite database

This commit is contained in:
localhost_frssoft 2022-08-31 17:46:05 +03:00
parent 838c25b639
commit 3f923cf2a6

35
src/fmn_states_db.py Normal file
View file

@ -0,0 +1,35 @@
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):
'''Создание стейта, если существует - будет заменен'''
c.execute("INSERT OR REPLACE INTO states(key, value) VALUES (?, ?)", (key, value))
conn.commit()
logger.warning(f'Добавлен стейт {key}: {value}')
def get_state(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}')