简体   繁体   English

如何使按钮将数据插入我的数据库?

[英]How to make a button insert data into my database?

I'm doing a little python product registration program and the database just doesn't open我正在做一点 python 产品注册程序,数据库就是打不开

I tried to open with sqlite3 library and by suggestion tried with QSqlDatabase, but nothing works.我尝试使用 sqlite3 库打开并建议尝试使用 QSqlDatabase,但没有任何效果。 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()

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).我建议使用QSqlDatabase创建与数据库的连接(它确实支持 SQLite 驱动程序)。 Using this instead of sqlite3 allows for better integration with the PyQt5 framework.使用它而不是sqlite3可以更好地与 PyQt5 框架集成。 Specifically, it can be easily hooked up with aQSqlTableModel and then displayed with aQTableView .具体来说,它可以很容易地与QSqlTableModel ,然后与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.它可能看起来比您当前使用的QTableWidget更加复杂和复杂,但它提供的简单数据库集成非常值得。

Excuse some of the doc links being for PySide2;请原谅一些用于 PySide2 的文档链接; the PyQt5 docs aren't exactly complete unfortunately.不幸的是,PyQt5 文档并不完全完整。

I solved the problem, with QSqlDatabase it did not accept and gave drivers error, so I went back to sqlite3.我解决了这个问题,使用 QSqlDatabase 它不接受并给驱动程序错误,所以我回到 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但是即使连接它仍然崩溃,所以我发现问题确实出在我没有转换为字符串的function

    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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM