-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
56 lines (44 loc) · 1.89 KB
/
db.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
import mysql.connector
import config
from Classes.reminder import Reminder
class Database():
def __init__(self):
self.mydb = mysql.connector.connect(
host=config.botConfig["host"],
user=config.botConfig["user"],
password=config.botConfig["password"],
port=config.botConfig["port"],
database=config.botConfig["database"],
charset='utf8mb4'
)
self.cursor = self.mydb.cursor()
# Check if the connection is alive
if self.mydb.is_connected():
print("Database connection successful")
else:
print("Database connection failed")
def add_new_reminder(self, reminder):
sql = f'INSERT INTO reminder VALUE(NULL, "{reminder.get_header()}", "{reminder.get_content()}", "{reminder.get_emails_formatted_sql()}", %s, %s);'
formatted_sql = sql.strip().replace('"', '\"')
values = (reminder.get_date_formatted_sql(), reminder.get_time_formatted_sql())
self.cursor.execute(formatted_sql, values)
self.mydb.commit()
def get_all_expired_reminders(self):
reminders = []
sql = f'select idReminder, header, content, emails from reminder where date < CURDATE() or (date = CURDATE() AND time < CURTIME());'
formatted_sql = sql.strip().replace('"', '\"')
self.cursor.execute(formatted_sql)
res = self.cursor.fetchall()
if res:
for row in res:
reminder = Reminder()
reminder.set_id(row[0])
reminder.set_header(row[1])
reminder.set_content(row[2])
reminder.set_emails(row[3])
reminders.append(reminder)
return reminders
def remove_reminder(self, idReminder):
sql = f"DELETE FROM reminder WHERE idReminder = {idReminder};"
self.cursor.execute(sql)
self.mydb.commit()