-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPPdatabase.py
More file actions
45 lines (39 loc) · 1.42 KB
/
Copy pathPPdatabase.py
File metadata and controls
45 lines (39 loc) · 1.42 KB
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 pymysql
import logging
# DataBase Connection
class DataBase:
con = None
def __init__(self):
self.connect()
def connect(self):
logging.info("Verbinde mit Datenbank...")
try:
self.con = pymysql.connect(host="localhost",
user="username",
passwd="password",
db="putzplan",
use_unicode=True,
charset="utf8")
except pymysql.DatabaseError as e:
logging.error("Verbindung mit Datenbank konnte nicht hergestellt werden! " + str(e))
return False
else:
logging.info("Mit Datenbank verbunden!")
return True
def query(self, sql):
try:
cursor = self.con.cursor(pymysql.cursors.DictCursor)
cursor.execute(sql)
self.con.commit()
except pymysql.OperationalError as e:
logging.warning("Die Query konnte nicht ausgeführt werden! " + str(e))
if (self.connect()):
cursor = self.con.cursor(pymysql.cursors.DictCursor)
cursor.execute(sql)
self.con.commit()
return cursor
def disconnect(self):
logging.info("Schließe Datenbankverbindung...")
if (self.con != None):
self.con.close()
logging.info("Datenbankverbindung geschlossen!")