-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython-mysql-crud.py
157 lines (136 loc) · 4.46 KB
/
python-mysql-crud.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import mysql.connector
from mysql.connector import Error
class Database:
"""
Database class.
Provides functionalities for database interactions.
Author: Mokter Hossain
Email: [email protected]
Website: www.gglink.uk
Github: https://github.com/moktermd08
Linkedin: https://www.linkedin.com/in/mr-mokter/
Twitter: https://twitter.com/moGGLink
"""
def __init__(self):
self.host = "localhost"
self.user = "user"
self.password = "password"
self.database = "database"
self.connection = None
def connect(self):
"""
Establishes a database connection.
Returns:
bool: True on successful connection, False otherwise
"""
try:
self.connection = mysql.connector.connect(
host=self.host,
user=self.user,
password=self.password,
database=self.database
)
if self.connection.is_connected():
return True
except Error as e:
print("Error:", e)
return False
def disconnect(self):
"""
Closes the database connection.
Returns:
bool: True on success, False otherwise
"""
if self.connection and self.connection.is_connected():
self.connection.close()
return True
return False
def select(self, table, columns='*', where=None):
"""
Executes a SELECT query.
Args:
table (str): The name of the table
columns (str, optional): The columns to select. Defaults to '*'.
where (str, optional): The WHERE clause. Defaults to None.
Returns:
list: The result rows as a list of dictionaries, empty list if query fails
"""
query = f"SELECT {columns} FROM {table}"
if where:
query += f" WHERE {where}"
try:
cursor = self.connection.cursor(dictionary=True)
cursor.execute(query)
return cursor.fetchall()
except Error as e:
print("Error:", e)
return []
def insert(self, table, values, columns=None):
"""
Inserts a new record into a table.
Args:
table (str): The name of the table
values (tuple): The data to insert
columns (str, optional): The columns for insert. Defaults to None.
Returns:
bool: True if insert was successful, False otherwise
"""
if columns:
query = f"INSERT INTO {table} ({columns}) VALUES ({', '.join(['%s'] * len(values))})"
else:
query = f"INSERT INTO {table} VALUES ({', '.join(['%s'] * len(values))})"
try:
cursor = self.connection.cursor()
cursor.execute(query, values)
self.connection.commit()
return True
except Error as e:
print("Error:", e)
return False
def update(self, table, set_data, where):
"""
Updates records in a table.
Args:
table (str): The name of the table
set_data (str): The data to update
where (str): The WHERE clause
Returns:
bool: True if update was successful, False otherwise
"""
query = f"UPDATE {table} SET {set_data} WHERE {where}"
try:
cursor = self.connection.cursor()
cursor.execute(query)
self.connection.commit()
return True
except Error as e:
print("Error:", e)
return False
def delete(self, table, where):
"""
Deletes records from a table.
Args:
table (str): The name of the table
where (str): The WHERE clause
Returns:
bool: True if delete was successful, False otherwise
"""
query = f"DELETE FROM {table} WHERE {where}"
try:
cursor = self.connection.cursor()
cursor.execute(query)
self.connection.commit()
return True
except Error as e:
print("Error:", e)
return False
# Example usage
if __name__ == "__main__":
db = Database()
if db.connect():
print("Connected to the database")
rows = db.select("users", "name, email", "age > 21")
print("Selected rows:", rows)
db.disconnect()
else:
print("Failed to connect to the database")