Skip to content

Commit 9b07c1e

Browse files
committed
Initial commit
0 parents  commit 9b07c1e

File tree

7 files changed

+472
-0
lines changed

7 files changed

+472
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.nox/
42+
.coverage
43+
.coverage.*
44+
.cache
45+
nosetests.xml
46+
coverage.xml
47+
*.cover
48+
.hypothesis/
49+
.pytest_cache/
50+
51+
# Translations
52+
*.mo
53+
*.pot
54+
55+
# Django stuff:
56+
*.log
57+
local_settings.py
58+
db.sqlite3
59+
60+
# Flask stuff:
61+
instance/
62+
.webassets-cache
63+
64+
# Scrapy stuff:
65+
.scrapy
66+
67+
# Sphinx documentation
68+
docs/_build/
69+
70+
# PyBuilder
71+
target/
72+
73+
# Jupyter Notebook
74+
.ipynb_checkpoints
75+
76+
# IPython
77+
profile_default/
78+
ipython_config.py
79+
80+
# pyenv
81+
.python-version
82+
83+
# celery beat schedule file
84+
celerybeat-schedule
85+
86+
# SageMath parsed files
87+
*.sage.py
88+
89+
# Environments
90+
.env
91+
.venv
92+
env/
93+
venv/
94+
ENV/
95+
env.bak/
96+
venv.bak/
97+
98+
# Spyder project settings
99+
.spyderproject
100+
.spyproject
101+
102+
# Rope project settings
103+
.ropeproject
104+
105+
# mkdocs documentation
106+
/site
107+
108+
# mypy
109+
.mypy_cache/
110+
.dmypy.json
111+
dmypy.json
112+
113+
# Pyre type checker
114+
.pyre/

ConnectionFactory.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import mysql.connector
2+
3+
4+
class ConnectionFactory:
5+
def getconnection(self):
6+
try:
7+
con = mysql.connector.connect(
8+
host="localhost",
9+
port="3306",
10+
user="root",
11+
passwd="root",
12+
database="contatodb"
13+
)
14+
except:
15+
print('Não foi possível realizar a conexão.')
16+
finally:
17+
return con
18+
19+
def closeconnection(self, con):
20+
con.close

Contato.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Contato:
2+
def __init__(self, id = None, nome = None, telefone = None, email = None):
3+
self.__id = id
4+
self.__nome = nome
5+
self.__telefone = telefone
6+
self.__email = email
7+
8+
def setid(self, id):
9+
self.__id = id
10+
11+
def getid(self):
12+
return self.__id
13+
14+
def setnome(self, nome):
15+
self.__nome = nome
16+
17+
def getnome(self):
18+
return self.__nome
19+
20+
def settelefone(self, telefone):
21+
self.__telefone = telefone
22+
23+
def gettelefone(self):
24+
return self.__telefone
25+
26+
def setemail(self, email):
27+
self.__email = email
28+
29+
def getemail(self):
30+
return self.__email

ContatoDAO.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
from ConnectionFactory import *
2+
from Contato import *
3+
4+
class ContatoDAO:
5+
def __init__(self):
6+
self.__cf = ConnectionFactory()
7+
8+
def add(self, contato):
9+
try:
10+
con = self.__cf.getconnection()
11+
if con.is_connected():
12+
cursor = con.cursor()
13+
cursor.execute("INSERT INTO contato(nome, telefone, email) VALUES(%s, %s, %s)", (contato.getnome(), contato.gettelefone(), contato.getemail()))
14+
con.commit()
15+
if cursor.rowcount > 0:
16+
print('Contato adicionado.')
17+
else:
18+
print('Contato não adicionado.')
19+
except:
20+
print('Não foi possível adicionar o contato.')
21+
con.rollback()
22+
finally:
23+
self.__cf.closeconnection(con)
24+
25+
def getcontato(self, id):
26+
try:
27+
con = self.__cf.getconnection()
28+
if con.is_connected():
29+
cursor = con.cursor()
30+
cursor.execute("SELECT * FROM contato WHERE id = %s", (id,))
31+
result = cursor.fetchone()
32+
if result is not None:
33+
contato = Contato(result[0], result[1], result[2], result[3])
34+
return contato
35+
else:
36+
print('Não existe contato com esse ID.')
37+
except:
38+
print('Não foi possível buscar o contato.')
39+
finally:
40+
self.__cf.closeconnection(con)
41+
42+
def getcontatospornome(self, nome):
43+
try:
44+
con = self.__cf.getconnection()
45+
if con.is_connected():
46+
cursor = con.cursor()
47+
cursor.execute("SELECT * FROM contato WHERE nome LIKE %s", ('%' + nome + '%',))
48+
result = cursor.fetchall()
49+
length = len(result)
50+
if length > 0:
51+
contatos = []
52+
for i in range(length):
53+
contatos.append(Contato(result[i][0], result[i][1], result[i][2], result[i][3]))
54+
return contatos;
55+
else:
56+
print('Não existem contatos com esse nome ou parte dele.')
57+
except:
58+
print('Não foi possível buscar os contatos.')
59+
finally:
60+
self.__cf.closeconnection(con)
61+
62+
def getcontatosportelefone(self, telefone):
63+
try:
64+
con = self.__cf.getconnection()
65+
if con.is_connected():
66+
cursor = con.cursor()
67+
cursor.execute("SELECT * FROM contato WHERE telefone LIKE %s", ('%' + telefone + '%',))
68+
result = cursor.fetchall()
69+
length = len(result)
70+
if length > 0:
71+
contatos = []
72+
for i in range(length):
73+
contatos.append(Contato(result[i][0], result[i][1], result[i][2], result[i][3]))
74+
return contatos;
75+
else:
76+
print('Não existem contatos com esse telefone ou parte dele.')
77+
except:
78+
print('Não foi possível buscar os contatos.')
79+
finally:
80+
self.__cf.closeconnection(con)
81+
82+
def getcontatosporemail(self, email):
83+
try:
84+
con = self.__cf.getconnection()
85+
if con.is_connected():
86+
cursor = con.cursor()
87+
cursor.execute("SELECT * FROM contato WHERE email LIKE %s", ('%' + email + '%',))
88+
result = cursor.fetchall()
89+
length = len(result)
90+
if length > 0:
91+
contatos = []
92+
for i in range(length):
93+
contatos.append(Contato(result[i][0], result[i][1], result[i][2], result[i][3]))
94+
return contatos;
95+
else:
96+
print('Não existem contatos com esse email ou parte dele.')
97+
except:
98+
print('Não foi possível buscar os contatos.')
99+
finally:
100+
self.__cf.closeconnection(con)
101+
102+
def getcontatos(self):
103+
try:
104+
con = self.__cf.getconnection()
105+
if con.is_connected():
106+
cursor = con.cursor()
107+
cursor.execute("SELECT * FROM contato")
108+
result = cursor.fetchall()
109+
length = len(result)
110+
if len(result) > 0:
111+
contatos = []
112+
for i in range(length):
113+
contatos.append(Contato(result[i][0], result[i][1], result[i][2], result[i][3]))
114+
return contatos;
115+
else:
116+
print('Não existem contatos.')
117+
except:
118+
print('Não foi possível buscar os contatos.')
119+
finally:
120+
self.__cf.closeconnection(con)
121+
122+
def update(self, contato):
123+
try:
124+
con = self.__cf.getconnection()
125+
if con.is_connected():
126+
cursor = con.cursor()
127+
cursor.execute("UPDATE contato SET nome = %s, telefone = %s, email = %s WHERE id = %s", (contato.getnome(), contato.gettelefone(), contato.getemail(), contato.getid()))
128+
con.commit()
129+
if cursor.rowcount > 0:
130+
print('Contato alterado com sucesso.')
131+
else:
132+
print('Não existe contato com esse ID.')
133+
except:
134+
print('Não foi possível alterar/atualizar o contato.')
135+
finally:
136+
self.__cf.closeconnection(con)
137+
138+
def delete(self, id):
139+
try:
140+
con = self.__cf.getconnection()
141+
if con.is_connected():
142+
cursor = con.cursor()
143+
cursor.execute("DELETE FROM contato WHERE id = %s", (id,))
144+
con.commit()
145+
if cursor.rowcount > 0:
146+
print('Contato deletado com sucesso.')
147+
else:
148+
print('Não existe contato com esse ID.')
149+
except:
150+
print('Não foi possível excluir/remover o contato.')
151+
finally:
152+
self.__cf.closeconnection(con)

0 commit comments

Comments
 (0)