Conexão com banco
This commit is contained in:
parent
641747634a
commit
247b2f4576
Binary file not shown.
|
|
@ -2,6 +2,7 @@ from flask import Flask, jsonify, request
|
|||
from flask_cors import CORS
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from db import get_connection
|
||||
|
||||
#Carrega arquivo de variáveis de ambiente (.env)
|
||||
load_dotenv()
|
||||
|
|
@ -19,6 +20,19 @@ CORS(app)
|
|||
def health():
|
||||
return jsonify({"message" : "API Flask version 1.0!!!"})
|
||||
|
||||
@app.route("/produtos",methods=["GET"])
|
||||
def listar_produtos():
|
||||
conexao = get_connection()
|
||||
cursor = conexao.cursor(dictionary=True)
|
||||
|
||||
cursor.execute("select * from produto")
|
||||
produtos = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
conexao.close()
|
||||
|
||||
return jsonify(produtos)
|
||||
|
||||
#Inicializa o servidor da APIRest
|
||||
if __name__ == "__main__":
|
||||
app.run(port=PORT, debug=True)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import os
|
||||
import mysql.connector
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
def get_connection():
|
||||
return mysql.connector.connect(
|
||||
host=os.getenv("DB_HOST"),
|
||||
user=os.getenv("DB_USER"),
|
||||
password=os.getenv("DB_PASSWORD"),
|
||||
database=os.getenv("DB_NAME"),
|
||||
port=os.getenv("DB_PORT")
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
#Criar tabela produto
|
||||
create table produto (
|
||||
id int auto_increment primary key,
|
||||
nome varchar(100) not null,
|
||||
preco decimal(10,2) not null,
|
||||
estoque int not null
|
||||
);
|
||||
|
||||
#Carga incial do banco
|
||||
insert into produto (nome,preco,estoque)
|
||||
values ('Bola',12.50,10);
|
||||
insert into produto (nome,preco,estoque)
|
||||
values ('PS5',3950.50,5);
|
||||
|
||||
#Exemplo de consulta
|
||||
#select * from produto;
|
||||
Loading…
Reference in New Issue