-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
51 lines (46 loc) · 1.65 KB
/
data.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
import pymysql
class DatabaseConnector:
def __init__(self, config):
self.host = config['host']
self.port = int(config['port'])
self.user = config['user']
self.password = config['password']
self.database = config['database']
self.connection = None
def connect(self):
try:
self.connection = pymysql.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
database=self.database
)
print("Connected to the database!")
except pymysql.Error as e:
print(f"Error connecting to the database: {e}")
def disconnect(self):
if self.connection:
self.connection.close()
print("Disconnected from the database.")
def execute_query(self, query, values=None):
try:
with self.connection.cursor() as cursor:
if values is None:
cursor.execute(query)
else:
cursor.execute(query, values)
result = cursor.fetchall()
return result
except pymysql.Error as e:
print(f"Error executing query: {e}")
def execute_insert(self, query, values):
try:
with self.connection.cursor() as cursor:
cursor.execute(query, values)
self.connection.commit()
print("Insert successful.")
return cursor.lastrowid
except pymysql.Error as e:
print(f"Error executing insert query: {e}")
self.connection.rollback()