-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conexao.py
99 lines (80 loc) · 2.76 KB
/
Conexao.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import mysql.connector
class Conexao:
def __init__(self, host, user, password, database):
self._host = host
self._user = user
self._password = password
self._database = database
self._con = None
self._cursor = None
def conectar(self):
try:
self._con = mysql.connector.connect(host=self._host, user=self._user, password=self._password, database = self._database)
except mysql.connector.Error as e:
print("Erro de SQL:", e)
except Exception as e:
print("Erro", e)
try:
self._cursor = self._con.cursor()
except mysql.connector.Error as e:
print("Erro de SQL:", e)
except Exception as e:
print("Erro", e)
def desconectar(self):
try:
self._cursor.close()
except mysql.connector.Error as e:
print("Erro de SQL:", e)
except Exception as e:
print("Erro", e)
try:
self._con.close()
except mysql.connector.Error as e:
print("Erro de SQL:", e)
except Exception as e:
print("Erro", e)
def consultar(self, sql):
self.conectar()
resultado = []
try:
self._cursor.execute(sql)
resultado = self._cursor.fetchall()
except mysql.connector.Error as e:
print("Erro de SQL:", e)
except Exception as e:
print("Erro", e)
self.desconectar()
return resultado
def consultarComParametros(self, sql, parametros):
self.conectar()
resultado = []
try:
self._cursor.execute(sql,parametros)
resultado = self._cursor.fetchall()
except mysql.connector.Error as e:
print("Erro de SQL:", e)
except Exception as e:
print("Erro", e)
self.desconectar()
return resultado
def manipular(self, sql):
self.conectar()
try:
self._cursor.execute(sql)
self._con.commit()
except mysql.connector.Error as e:
print("Erro de SQL:", e)
except Exception as e:
print("Erro:", e)
self.desconectar()
#Criar o manipular com parâmetros
def manipularComParametros(self, sql, parametros):
self.conectar()
try:
self._cursor.execute(sql, parametros)
self._con.commit()
except mysql.connector.Error as e:
print("Erro de SQL:", e)
except Exception as e:
print("Erro:", e)
self.desconectar()