-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconexao.py
More file actions
41 lines (36 loc) · 1.38 KB
/
conexao.py
File metadata and controls
41 lines (36 loc) · 1.38 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
import pymongo
class MongoConnection():
def __init__(self):
try:
self.client = pymongo.MongoClient("localhost", 27017)
self.estoque = self.client["estoque"]
self.produto = self.estoque["produto"]
# Login padrão do MongoDB, database 'estoque' e collection 'produto'
except Exception as e:
print(f"Erro ao estabelecer conexão: {e}")
def db_read(self, query=None, projection=None):
try:
leitura = str("")
for prd in self.produto.find(query, projection):
for key, value in prd.items():
leitura += "\n" + str(key) + ": " + str(value)
leitura += "\n"
except Exception as e:
print(f"Erro ao ler registro: {e}")
finally:
return leitura
def db_save(self, json):
try:
self.produto.insert_one(json)
except Exception as e:
print(f"db_save - Erro ao salvar registro: {e} \n {json}")
def db_update(self, query, field):
try:
self.produto.update(query, field)
except Exception as e:
print(f"Erro ao atualizar registro: {e} \n {query}, {field}")
def db_remove(self, query):
try:
self.produto.remove(query)
except Exception as e:
print(f"Erro ao excluir registro: {e} \n {query}")