From 3f923cf2a66867de6bb6589f2bdf6919c1006d61 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Wed, 31 Aug 2022 17:46:05 +0300 Subject: [PATCH] States storage as sqlite database --- src/fmn_states_db.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/fmn_states_db.py diff --git a/src/fmn_states_db.py b/src/fmn_states_db.py new file mode 100644 index 0000000..7210ea1 --- /dev/null +++ b/src/fmn_states_db.py @@ -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}') +