Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Pequenos ajustes
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonmello committed Jun 7, 2018
1 parent df8c4f4 commit 80312fc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,4 @@ venv.bak/


# End of https://www.gitignore.io/api/macos,python,pycharm
.vscode/
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/bcd29008/python-flask-mysql/master/LICENSE)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

# Exemplo de python Flask e MySQL

Expand Down
22 changes: 9 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# http://flask.pocoo.org/docs/1.0/tutorial/
# g is a special object that is unique for each request.
# It is used to store data that might be accessed by multiple functions during the request.
Expand All @@ -14,7 +17,7 @@
'password': 'senha',
'host': '127.0.0.1',
'database': 'agenda',
'raise_on_warnings': True,
'raise_on_warnings': True
}

SECRET_KEY = 'aula de BCD - string aleatória'
Expand Down Expand Up @@ -84,27 +87,20 @@ def editar():
cid = str(request.args.get('id'))
g.db = MySQLConnection(**db_config)
cursor = g.db.cursor(prepared=True)
consulta = ("SELECT * FROM Contato WHERE cID = %s")
consulta = ("SELECT nome, email FROM Contato WHERE cID = %s")
cursor.execute(consulta, (cid))
linha = cursor.fetchone()
contato = [
{
'id' : linha[0],
'nome' : linha[1],
'email' : linha[2]
}
]
cursor.close()
g.db.close()

session['cid'] = cid;
return render_template('editar.html', title='Editar contato', contato=contato)
session['cid'] = cid
return render_template('editar.html', title='Editar contato', contato=linha)

else:
cid = session['cid']
nome = request.form['nome']
email = request.form['email']
session.pop('cId', None)
session.pop('cid', None)

g.db = MySQLConnection(**db_config)
cursor = g.db.cursor(prepared=True)
Expand All @@ -120,4 +116,4 @@ def editar():


if __name__ == '__main__':
app.run()
app.run(debug=True)
9 changes: 9 additions & 0 deletions templates/agenda-ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE DATABASE agenda;
USE agenda;

CREATE TABLE Contato (
cId int(11) NOT NULL AUTO_INCREMENT,
nome varchar(100) DEFAULT NULL,
email varchar(100) DEFAULT NULL,
PRIMARY KEY (cId)
);
6 changes: 2 additions & 4 deletions templates/editar.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
<h2>Editar contato</h2>
{% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}
<form action="{{ url_for('editar') }}" method=post>
{% for c in contato %}
<P>Nome:
<BR><input type=text name=nome value="{{ c.nome }}" required>
<BR><input type=text name=nome value="{{ contato[0] }}" required>
<P>Email:
<BR><input type=text name=email value="{{ c.email }}" required>
<BR><input type=text name=email value="{{ contato[1] }}" required>
<input type=submit value="Atualizar">
{% endfor %}
</form>
{% endblock %}
8 changes: 4 additions & 4 deletions templates/listar.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ <h2>Listar contato</h2>
<th>Nome</th>
<th>email</th>
</tr>
{% for contato in contatos %}
{% for pessoa in contatos %}
<tr>
<td><a href='/editar?id={{ contato.id }}'>{{ contato.id }}</a></td>
<td>{{ contato.nome }}</td>
<td>{{ contato.email }}</td>
<td><a href='/editar?id={{ pessoa.id }}'>{{ pessoa.id }}</a></td>
<td>{{ pessoa.nome }}</td>
<td>{{ pessoa.email }}</td>
</tr>
{% endfor %}
</table>
Expand Down

0 comments on commit 80312fc

Please sign in to comment.