-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL.py
45 lines (33 loc) · 1.17 KB
/
MySQL.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
import mysql.connector as mysql
class ConnectionError(Exception): ...
class MySQLHandler:
def __init__(self, conn: mysql.MySQLConnection):
self.conn = conn
self.cursor = conn.cursor()
def execute(self, query, params = tuple(), rows = None, multi = False):
self.cursor.execute(query, params, multi)
if rows is None:
return self.cursor.fetchall()
else:
return self.cursor.fetchmany(rows)
def commit(self):
self.conn.commit()
def rollback(self):
self.conn.rollback()
def columnNames(self):
return self.cursor.column_names
def close(self):
self.cursor.close()
self.conn.close()
class MySQL:
def __init__(self, *args, **kwargs):
self.conn = mysql.connect(*args, **kwargs)
self.__handler = None
if not self.conn.is_connected():
raise ConnectionError("Couldn't connect to database")
def __enter__(self):
self.__handler = MySQLHandler(self.conn)
return self.__handler
def __exit__(self, type, value, traceback):
self.__handler.close()
self.__handler = None