-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLConnector.py
57 lines (46 loc) · 1.96 KB
/
MySQLConnector.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
import mysql.connector as connector
import logging
# Save when disconnected from database server
SAVE_ON_DISCONNECT = True
# Logging
logging.basicConfig(filename='database.log', format='%(asctime)s - Process ID:%(process)d ---- %(levelname)s ---- %(message)s', datefmt='%d-%b-%y %H:%M:%S')
class mysqlDB():
def __init__(self, MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE):
self.host = MYSQL_HOST
self.username = MYSQL_USERNAME
self._password = MYSQL_PASSWORD
self.database = MYSQL_DATABASE
self.connection = 0
self.connectDB()
self.cursor = self.getNewCursor() if self.getConnection().is_connected() else False
def save(self):
self.connection.commit()
def rollback(self):
self.connection.rollback()
def getConnection(self):
return self.connection
def getCursor(self):
return self.cursor
def connectDB(self):
try:
connection = connector.connect(host = self.host, user = self.username, passwd = self._password, database = self.database)
logging.debug(f'Connection to Database: {self.database} was successful')
except:
logging.error(f'Connection to Database: {self.database} was unsuccessful')
self.connection = connection
def disconnectDB(self, save=1):
if self.getConnection() is not False:
# save
if save: self.save()
# disconnect
self.cursor.close()
self.connection.close()
self.connection = False
# logging.debug('Connection to database has been stopped')
def getNewCursor(self):
try:
self.connection.ping(reconnect=True, attempts=3, delay=4)
except connector.Error:
logging.debug(f'Connection to Database: {self.database} was unsuccessful')
self.connection = self.connectDB()
return self.connection.cursor(buffered=True)