-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
114 lines (100 loc) · 3.21 KB
/
Copy pathdatabase.py
File metadata and controls
114 lines (100 loc) · 3.21 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sqlite3
import os
from dotenv import load_dotenv
load_dotenv()
class database(object):
def __init__(self):
self.PATH = os.environ["DATABASE_PATH"]
def connect(self): # connects to the database
try:
self.conn = sqlite3.connect(self.PATH)
return print("Database Connected")
except:
return print("Database not Connected")
def setup(
self,
): # creates a new BDMemo table (used manually after a reset)
self.connect()
cur = self.conn.cursor()
cur.execute(
"""
CREATE TABLE BDMemo
(
NAME TEXT NOT NULL,
BIRTHDAY TEXT NOT NULL,
CHAT_ID TEXT NOT NULL
)
"""
)
cur.execute(
"""
CREATE TABLE Settings
(
CHAT_ID TEXT NOT NULL,
REMINDER_DAYS TEXT NOT NULL
)
"""
)
self.conn.commit()
self.conn.close()
return print("Tables created")
def insert_data(self, name, birthday, chat_id): # insert data into the table
self.connect()
cur = self.conn.cursor()
cur.execute(
f"INSERT INTO BDMemo (NAME, BIRTHDAY, CHAT_ID) VALUES ('{name}', '{birthday}', '{chat_id}')"
)
self.conn.commit()
self.conn.close()
return print("Data Stored")
def insert_settings(self, chat_id, reminder_days):
self.connect()
cur = self.conn.cursor()
cur.execute(
f"INSERT INTO Settings (CHAT_ID, REMINDER_DAYS) VALUES ('{chat_id}', '{reminder_days}')"
)
self.conn.commit()
self.conn.close()
return print("Settings stored")
def edit_settings(self, chat_id, reminder_days):
self.connect()
cur = self.conn.cursor()
cur.execute(
f"UPDATE Settings SET REMINDER_DAYS = '{reminder_days}' WHERE CHAT_ID = '{chat_id}'"
)
self.conn.commit()
self.conn.close()
return print(f"Settings changed: [{reminder_days}]")
def delete_data(self, name, chat_id): # delete data into the table
self.connect()
cur = self.conn.cursor()
cur.execute(
f"DELETE FROM BDMemo WHERE (NAME, CHAT_ID) = ('{name}', '{chat_id}')"
)
self.conn.commit()
self.conn.close()
return print("Data deleted")
def extract_data(self): # get all the data from the table
self.connect()
cur = self.conn.cursor()
cur.execute("SELECT NAME, BIRTHDAY, CHAT_ID FROM BDMemo")
rows = cur.fetchall()
self.conn.close()
print(f"Data extracted: {rows}")
return rows
def extract_settings(self):
self.connect()
cur = self.conn.cursor()
cur.execute("SELECT CHAT_ID, REMINDER_DAYS FROM Settings")
rows = cur.fetchall()
self.conn.close()
print("Settings extracted")
return rows
def get_list(self, chat_id): # get the data stored from a user
self.connect()
cur = self.conn.cursor()
cur.execute(f"SELECT NAME, BIRTHDAY FROM BDMemo WHERE CHAT_ID = '{chat_id}'")
list = cur.fetchall()
self.conn.close()
return list
db = database()