From 80312fcfc7cc82543dbf8a61020fffc3be200835 Mon Sep 17 00:00:00 2001 From: Emerson Ribeiro de Mello Date: Thu, 7 Jun 2018 10:49:59 -0300 Subject: [PATCH] Pequenos ajustes --- .gitignore | 1 + Readme.md | 2 +- app.py | 22 +++++++++------------- templates/agenda-ddl.sql | 9 +++++++++ templates/editar.html | 6 ++---- templates/listar.html | 8 ++++---- 6 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 templates/agenda-ddl.sql diff --git a/.gitignore b/.gitignore index e29a44a..d40cd74 100644 --- a/.gitignore +++ b/.gitignore @@ -195,3 +195,4 @@ venv.bak/ # End of https://www.gitignore.io/api/macos,python,pycharm +.vscode/ diff --git a/Readme.md b/Readme.md index c3ef4f6..10a5851 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/app.py b/app.py index 3188b62..3d24c83 100644 --- a/app.py +++ b/app.py @@ -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. @@ -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' @@ -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) @@ -120,4 +116,4 @@ def editar(): if __name__ == '__main__': - app.run() + app.run(debug=True) diff --git a/templates/agenda-ddl.sql b/templates/agenda-ddl.sql new file mode 100644 index 0000000..8514662 --- /dev/null +++ b/templates/agenda-ddl.sql @@ -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) +); \ No newline at end of file diff --git a/templates/editar.html b/templates/editar.html index df5e1b0..bbca22b 100644 --- a/templates/editar.html +++ b/templates/editar.html @@ -3,12 +3,10 @@

Editar contato

{% if error %}

Error: {{ error }}{% endif %}

- {% for c in contato %}

Nome: -
+

Email: -
+
- {% endfor %}

{% endblock %} \ No newline at end of file diff --git a/templates/listar.html b/templates/listar.html index c89e00e..57e14e6 100644 --- a/templates/listar.html +++ b/templates/listar.html @@ -8,11 +8,11 @@

Listar contato

Nome email - {% for contato in contatos %} + {% for pessoa in contatos %} - {{ contato.id }} - {{ contato.nome }} - {{ contato.email }} + {{ pessoa.id }} + {{ pessoa.nome }} + {{ pessoa.email }} {% endfor %}