-
Notifications
You must be signed in to change notification settings - Fork 28
/
database.py
72 lines (65 loc) · 2.25 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import logging
import sqlite3
from sqlite3 import Error
from config import inbox_path
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO, filename = 'bot.log', encoding = 'UTF-8', datefmt = '%Y-%m-%d %H:%M:%S')
def create_connection():
"""Create a database connection to the SQLite database"""
try:
conn = sqlite3.connect('bot_settings.db')
return conn
except Error as e:
logging.error(f"Error connecting to database: {e}")
return None
def init_database():
"""Create the settings table if it doesn't exist"""
conn = create_connection()
if conn is not None:
try:
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS chat_settings
(chat_id INTEGER PRIMARY KEY,
notes_folder TEXT NOT NULL)
''')
conn.commit()
except Error as e:
logging.error(f"Error creating table: {e}")
finally:
conn.close()
def set_notes_folder(chat_id, folder_path) -> str:
# Save to database
conn = create_connection()
if conn is not None:
try:
c = conn.cursor()
c.execute('''
INSERT OR REPLACE INTO chat_settings (chat_id, notes_folder)
VALUES (?, ?)
''', (chat_id, folder_path))
conn.commit()
result = f"Notes folder for {chat_id} set to: {folder_path}"
logging.info(result)
except Error as e:
result = f"Error saving settings for for {chat_id}: {e}"
logging.error(result)
finally:
conn.close()
return result
def get_notes_folder(chat_id) -> str:
conn = create_connection()
folder_path = ""
if conn is not None:
try:
c = conn.cursor()
c.execute('SELECT notes_folder FROM chat_settings WHERE chat_id = ?',
(chat_id,))
row = c.fetchone()
if row:
folder_path = row[0]
except Error as e:
logging.error(f"Database error: {e}")
finally:
conn.close()
return folder_path
init_database()