-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
executable file
·75 lines (69 loc) · 2.42 KB
/
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
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
# IMPORTS
import os
import mysql.connector as mariadb
from helpers.functions import *
class SQL:
# to set the database values
def __init__(self, host, port, user, password, database):
self.host = host
self.port = port
self.user = user
self.password = password
self.database = database
# to connect to the database
def connect(self):
self.connection = mariadb.connect(
host=self.host, port=self.port, user=self.user, password=self.password, database=self.database)
self.cursor = self.connection.cursor()
# to execute a query
def run(self, query, fetch=True, close=True):
result = False
try:
self.cursor.execute(query)
except:
try:
self.connect()
except:
return {"message": "Database error"}
try:
self.cursor.execute(query)
except mariadb.errors.DataError:
result = {"message": "Data too long"}
except mariadb.errors.IntegrityError as e:
try:
repeatedName = str(e).split("'")[1].split('-')[0]
result = {"message": "Repeated data", "id": False, "name": repeatedName}
except IndexError:
print(e)
except Exception as e:
print(e)
result = {"message": "Database error"}
if fetch == True and not result:
try:
result = self.cursor.fetchall()
result = toJSON(self.cursor, result)
except Exception as e:
print(e)
result = {"message": "Database error"}
elif not result:
result = {"message": "good"}
try:
result['id'] = self.cursor.lastrowid
except:
pass
try:
self.connection.commit()
except Exception as e:
result = {"message": "Database Error"}
print(e)
if close == True:
try:
self.close()
except:
result = {"message": "Database error"}
return result
# to close the database
def close(self):
self.cursor.close()
self.connection.close()
sql = SQL(os.environ["DB_HOST"], os.environ["DB_PORT"], os.environ["DB_USER"], os.environ["DB_PW"], os.environ["DB_DB"])