-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
51 lines (44 loc) · 1.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
import pymysql
import sys
class Database:
def __init__(self, server, username, password, database):
self.server = server
self.username = username
self.password = password
self.database_name = database
self.db = None
def connect(self):
try:
# Connection to the MySQL database
self.db = pymysql.connect(
self.server, self.username, self.password, self.database_name)
except Exception:
print("Invalid username or password")
sys.exit(1)
def close(self):
if self.db.open:
self.db.close()
@staticmethod
def retrieve(cursor, flag):
if flag == "1":
column_names = [i[0] for i in cursor.description]
print("!!!!!!!!!!!!", column_names)
result = list()
temp_array = []
for row in cursor:
if len(row) == 1:
result.append(str(row[0]))
else:
for i in range(0, len(row)):
temp_array.append(str(row[i]))
result.append(temp_array)
temp_array = []
if flag == "0":
return result
else:
return result, column_names
def execute_query(self, sql_query, flag="0"):
cursor = self.db.cursor()
cursor.execute(sql_query)
self.db.commit()
return Database.retrieve(cursor, flag)