-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionHelper.py
46 lines (37 loc) · 1.33 KB
/
ConnectionHelper.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 pymysql
import os
#MySQL connection
class ConnectionHelper:
entrada = []
def __init__(self):
self.file = open("credential.txt","r")
with open ("credential.txt", "r") as myfile:
for line in myfile:
self.entrada.append(line)
self.mysql_user = self.entrada[0].strip()
self.mysql_password = self.entrada[1].strip()
self.connection_options = {
'host': 'localhost',
'user': self.mysql_user,
'password': self.mysql_password,
'database': 'fmp_db',
'autocommit':True
}
self.connection = pymysql.connect(**self.connection_options)
def run(self, query, args=None):
result_list = []
with self.connection.cursor() as cursor:
print('Executando query:')
print(cursor.mogrify(query, args))
cursor.execute(query, args)
for result in cursor.fetchall():
result_list.append(result)
return result_list
def callproc(self, query, args):
result_list = []
with self.connection.cursor() as cursor:
print('Executando query:')
cursor.callproc(query, args)
for result in cursor.fetchall():
result_list.append(result)
return result_list