-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
40 lines (33 loc) · 1008 Bytes
/
database.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
import mysql.connector
class Database:
def __init__(self):
# Connect to MySQL database
self.conn = mysql.connector.connect(
host='localhost',
user='root',
password='1234',
database='chs'
)
self.cursor = self.conn.cursor()
def execute_query(self, query, data=None):
# Execute SQL query
if data:
self.cursor.execute(query, data)
else:
self.cursor.execute(query)
def fetch_one(self):
# Fetch one record
return self.cursor.fetchone()
def fetch_all(self):
# Fetch all records
return self.cursor.fetchall()
def commit(self):
# Commit transaction
self.conn.commit()
def rollback(self):
# Rollback transaction
self.conn.rollback()
def close_connection(self):
# Close database connection
self.cursor.close()
self.conn.close()