-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmysql_class.py
116 lines (89 loc) · 3.44 KB
/
mysql_class.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
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
115
116
import mysql.connector
import pandas as pd
from dotenv import load_dotenv
load_dotenv()
import os
host = os.getenv("host")
user = os.getenv("user")
password = os.getenv("password")
database = os.getenv("database")
class MySQLDatabase:
def __init__(self):
self.connection = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
)
self.cursor = self.connection.cursor()
def __del__(self):
self.connection.close()
def insert_data(self, table, columns, values):
query = f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({', '.join(['%s' for _ in values])})"
try:
self.cursor.execute(query, values)
self.connection.commit()
print("Data inserted successfully")
# Retrieve the latest created ID
last_id = self.cursor.lastrowid
print(f"Last inserted ID: {last_id}")
return last_id
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def fetch_all_rows(self, table):
query = f"SELECT * FROM {table}"
try:
self.cursor.execute(query)
rows = self.cursor.fetchall()
if rows:
columns = [desc[0] for desc in self.cursor.description]
df = pd.DataFrame(rows, columns=columns)
return df
else:
print(f"No data found in table {table}")
return None
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def get_row_count(self, table):
query = f"SELECT COUNT(*) FROM {table}"
try:
self.cursor.execute(query)
count = self.cursor.fetchone()[0]
return count
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
# db=MySQLDatabase()
# db.insert_data('booking', ['name'], ['SarmadAFzal'])
def cancelled_count(self):
query = f"SELECT count(*) FROM booking where is_cancel = 1"
try:
self.cursor.execute(query)
count = self.cursor.fetchone()[0]
return count
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def update_data(self, table, column, value, condition_column=None, condition_value=None):
"""
Update data in the specified table.
Parameters:
- table (str): The name of the table.
- column (str): The column to be updated.
- value: The new value to set in the specified column.
- condition_column (str, optional): The column for the WHERE condition.
- condition_value (any, optional): The value for the WHERE condition.
"""
query = f"UPDATE {table} SET {column} = %s"
params = [value]
if condition_column and condition_value:
query += f" WHERE {condition_column} = %s"
params.append(condition_value)
try:
self.cursor.execute(query, params)
self.connection.commit()
print("Data updated successfully")
except mysql.connector.Error as err:
print(f"Error: {err}")