Python does not insert data in sqlite3

Asked

Viewed 326 times

0

I have the following code:

#!/usr/bin/python3

import sqlite3

app = '/backup/app/'
bd = app + 'bd/'
contato_banco = bd + 'contato.db'
contato_tab = (('nome' , 'TEXT'), ('email' , 'TEXT'), ('whatsapp' , 'INT'), ('mensagem' , 'TEXT'))
c = 'Enio', '[email protected]', '+5513991874221', 'Alguma coisa'

def conexão(banco, comando):
    conexao = sqlite3.connect(bd + banco)
    cursor = conexao.cursor()
    return(cursor.execute(comando))

def cria_tabela(bd, tabela, campos):
    campo = ''
    for unidade in range(0, len(campos)):
        campo += '"' + campos[unidade][0] + '" ' + campos[unidade][1] + ', '
    return(conexão(bd, f'CREATE TABLE {tabela} ({campo[:-2]})'))

def insere_dado(banco, tabela, dados):
    global conexao, cursor
    saida = ''
    for campo in dados:
        saida += '"' + campo + '", '
    comando = (f'INSERT INTO {tabela} VALUES ({saida[:-2]})')
    conexão(banco, comando)
    sqlite3.connect(banco).commit()

insere_dado('teste.db', 'contato', c)

python does not present me any error, however it does not save in the table.

  • The bank /backup/app/bd/teste exists?

  • sqlite3.connect(bd + bank) # creates the database if it does not exist

  • The creati_table() function works perfectly as well.

1 answer

2


Missed the commit to save bank changes.

...
result = cursor.execute(comando)
conexao.commit()  # Salvar
return result

Browser other questions tagged

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