mirror of
https://gitea.phreedom.club/localhost_frssoft/FMN_bot
synced 2024-11-01 05:29:20 +02:00
States storage as sqlite database
This commit is contained in:
parent
838c25b639
commit
3f923cf2a6
|
@ -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}')
|
||||||
|
|
Loading…
Reference in New Issue