-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_helper.py
62 lines (51 loc) · 1.79 KB
/
mysql_helper.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
import mysql.connector
from typing import Optional
class MySqlDatabase:
def __init__(self, host, user, password, database):
self.host = host
self.user = user
self.password = password
self.database = database
self.connection = None
self.cursor = None
def connect(self):
self.connection = mysql.connector.connect(
host=self.host,
user=self.user,
password=self.password,
database=self.database
)
if not self.connection.is_connected():
raise Exception("Could not connect to database")
self.cursor = self.connection.cursor(buffered=True)
self.create_task_table()
def create_task_table(self):
self.execute("""
CREATE TABLE IF NOT EXISTS todo (
id INT AUTO_INCREMENT PRIMARY KEY,
task_type VARCHAR(255),
task VARCHAR(255),
requested_by VARCHAR(255),
completed BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
def disconnect(self):
if self.connection:
self.connection.close()
def execute(self, query: str, values: Optional[list] = None):
if not self.connection:
self.connect()
self.cursor.execute(query, values)
self.connection.commit()
return self.cursor.lastrowid
def fetch(self, query, values=None):
if not self.connection:
self.connect()
self.cursor.execute(query, values)
return self.cursor.fetchall()
def fetch_one(self, query, values=None):
if not self.connection:
self.connect()
self.cursor.execute(query, values)
return self.cursor.fetchone()