How to make a button insert data into my database?

-1

I'm doing a little python product registration program and the database just doesn't open

I tried to open with sqlite3 library and by suggestion tried with QSqlDatabase, but nothing works. What could this error be? How to solve and connect?

i changed the database connection

path = r'C:\Users\Daniel\Desktop\Sistema NaruHodo\Banco de Dados'
conn = sqlite3.connect(path+r'\produtos.db')
cursor = conn.cursor()

Erro:

C:\Users\Daniel\AppData\Local\Programs\Python\Python37\pythonw.exe "C:/Users/Daniel/Desktop/Sistema NaruHodo/cadastroprodutos.py"

Process finished with exit code -1073740791 (0xC0000409)

Here is the def I'm using to try to get the field data and insert it into the database.

    def addProduto(self, produtotext, estoquetext, precocustotext, precovendatext, fornecedorcomboBox):
        path = r'C:\Users\Daniel\Desktop\Sistema NaruHodo\Banco de Dados'
        conn = sqlite3.connect(path+r'\produtos.db')
        produto = str(self.produtotext.text())
        estoque = float(self.estoquetext.text())
        precocusto = float(self.precocustotext.text())
        precovenda = float(self.precovendatext.text())
        fornecedor = str(self.fornecedorcomboBox.currentText())
        conn.execute(f"""INSERT INTO produ VALUES (null, {produto}, {estoque}, {precocusto}, {precovenda}, {fornecedor})""")
        conn.commit()
python
sqlite
pyqt5
asked on Stack Overflow Oct 15, 2019 by Daniel Oliveira • edited Oct 18, 2019 by eyllanesc

2 Answers

0

It looks like you're trying to display the database in a table. I recommend using a QSqlDatabase to create a connection to the database (it does support an SQLite driver). Using this instead of sqlite3 allows for better integration with the PyQt5 framework. Specifically, it can be easily hooked up with a QSqlTableModel and then displayed with a QTableView. For this approach, I suggest you familiarise yourself with model/view programming. It may seem more involved and complex than the QTableWidget you are currently using, but it's well worth it for the easy database integration it offers.

Excuse some of the doc links being for PySide2; the PyQt5 docs aren't exactly complete unfortunately.

answered on Stack Overflow Oct 15, 2019 by bgfvdu3w
0

I solved the problem, with QSqlDatabase it did not accept and gave drivers error, so I went back to sqlite3. But it was still crashing even connected, so I found that the problem was really in the function that I didn't convert to string

    def addProduto(self):
        self.banco = sqlite3.connect('Vendas.db')
        self.cursor = banco.cursor()
        Nome = self.produtotext.text()
        Quant = self.estoquetext.text()
        Valor = self.precocustotext.text()
        Preco = self.precovendatext.text()
        Forn = self.fornecedorcomboBox.currentText()
        if Nome != '' or Quant != '' or Valor != '' or Preco != '' or Forn != '':
            self.banco.execute(f"""INSERT INTO Produtos VALUES ('{Nome}',{Quant}, {Valor}, {Preco}, '{Forn}')""")
            self.banco.commit()
            self.LoadDatabase()
            self.limparcampos()
answered on Stack Overflow Oct 18, 2019 by Daniel Oliveira

User contributions licensed under CC BY-SA 3.0