Home > Net >  Python does not execute the def function
Python does not execute the def function

Time:09-23

I'm trying to use an api, but when the code arrives in the def function it stops executing

import requests
import sqlite3
from datetime import date

conn = sqlite3.connect('bdcotacoes.db')
print('banco de dados aberto')
cursor = conn.cursor()
requests = requests.get("https://api.hgbrasil.com/finance?format=json-cors&key=b0e3f64f")
dados = requests.json()
data = date.today()
data_formato = '{}-{}-{}'.format(data.day, data.month, data.year)

Dolar = dados['results']['currencies']['USD']['buy']
Euro = dados['results']['currencies']['EUR']['buy']


def inserir():
    conn.execute("INSERT INTO moedas (Data, Dolar, Euro) VALUES(?, ?, ?)", (data_formato, Dolar, Euro))
    conn.commit()
    print('Dados inseridos com sucesso')
    conn.close()

CodePudding user response:

You need to call the function so that it runs -

...
def inserir():
    conn.execute("INSERT INTO moedas (Data, Dolar, Euro) VALUES(?, ?, ?)", (data_formato, Dolar, Euro))
    conn.commit()
    print('Dados inseridos com sucesso')
    conn.close()

inserir() # Call Functions

In your code, you are only defining the function but it would not run if you do not call it.

  • Related