Sqlite - Python insert data automatically

Asked

Viewed 306 times

1

Hi, I’m writing an algorithm that collects information from dividend companies. The collection is performed and mounts a table and up to this point everything excellent. but I would like to save in a database and am using Python Sqlite 3. The table is perfect, the problem is when I try to insert in the bank. I believe the error is in the "FOR" loop, but I still can’t unravel.

connection = sqlite3.connect('dividendos.db')
con = connection.cursor()


def create_table():
    con.execute('CREATE TABLE IF NOT EXISTS dados (Empresa text, Tipo text, Data_Ex text, Data_Pag text, Valor text)')

create_table()

def data_entry():
    
        for empresa in tabela['Empresa']:
            empresa = str(empresa)

        for tipo in tabela['Tipo']:
            tipo = str(tipo)
    
            con.execute("INSERT INTO dados VALUES('"+empresa+"','"+tipo+"','DataEx','DataPag','Valor')")
        connection.commit()
data_entry()

No errors returned, just not saving as it should. I can do only one "FOR" to read the whole table, go assigning and only then enter the values in the database?

1 answer

1


If using the pandas to store the database values, you can do as follows:

Importing the necessary packages

import pandas as pd
import sqlite3

Simulating incoming data and creating a data frame

empresa = ['EMPRESA_A','EMPRESA_B','EMPRESA_C']
tipo = ['TIPO_A','TIPO_B','TIPO_C']
dataex = ['2019','2019','2020']
datapag = ['01-01-2019','02-01-2019','03-01-2020']
valor = [10, 20, 30]

tabela = pd.DataFrame({'empresa': empresa, 'tipo': tipo, 'Data_Ex': dataex, 'Data_Pag': datapag, 'Valor': valor})

Maintaining the structure of your code to create a db and table

connection = sqlite3.connect('dividendos.db')
con = connection.cursor()

def create_table():
    con.execute('CREATE TABLE IF NOT EXISTS dados (Empresa text, Tipo text, Data_Ex text, Data_Pag text, Valor text)')

create_table()

With this line you add the content of the data frame to the database

tabela.to_sql(name = 'dados', con = connection, if_exists = 'append', index = False)

With this line you can recover what is on the table

back = pd.read_sql('select * from dados', connection)

Code:

import pandas as pd
import sqlite3

# simulando dados recebidos e criando um data frame
empresa = ['EMPRESA_A','EMPRESA_B','EMPRESA_C']
tipo = ['TIPO_A','TIPO_B','TIPO_C']
dataex = ['2019','2019','2020']
datapag = ['01-01-2019','02-01-2019','03-01-2020']
valor = [10, 20, 30]

tabela = pd.DataFrame({'empresa': empresa, 'tipo': tipo, 'Data_Ex': dataex, 'Data_Pag': datapag, 'Valor': valor})

connection = sqlite3.connect('dividendos.db')
con = connection.cursor()

def create_table():
    con.execute('CREATE TABLE IF NOT EXISTS dados (Empresa text, Tipo text, Data_Ex text, Data_Pag text, Valor text)')

create_table()

tabela.to_sql(name = 'dados', con = connection, if_exists = 'append', index = False)

back = pd.read_sql('select * from dados', connection)

back

Exit:

    empresa      tipo   Data_Ex Data_Pag    Valor
0   EMPRESA_A   TIPO_A  2019    01-01-2019  10
1   EMPRESA_B   TIPO_B  2019    02-01-2019  20
2   EMPRESA_C   TIPO_C  2020    03-01-2020  30
  • In the column company will receive from the table table[2ª Company']. So that way you put it works ?

  • 1

    I didn’t quite understand the question... I added at the end of the answer an example data frame. That’s how it will be inserted and returned. If your data frame is different, the code tabela.to_sql works because it saves the data frame.

  • I assign the values of the variables and they look like this: company = table['Company'] type = table['Type'] dataex = table['Dataex'] datapag = table['Prevpag'] value = table['Value'] but in the end gave this error: Valueerror: could not broadcast input array from Shape (2) into Shape (19)

  • 1

    The way I posted the answer, you already created the fields in the database, right? What pd.to_sql does is associate the column names with the table columns. So it records without you having to do these assignments in separate variables. Did you test the code I posted? If you have any questions post your dataset I try to help you. Hug!

  • It worked, it was perfect! And much simpler. Now just one more thing, is it possible to write ? Instead of adding lines, stay only the new ? Or rather, save only if the line is not repeated.

  • 1

    Documentation, you can check to_sql methods and options. In this case ai can use instead of append the replace where is the if_exists. If the answer has solved your problem, consider marking as aceita. Big Hug!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.