到目前为止,Python的进展还不错。我需要这段代码的帮助:
def btn_clk(self):
sender = self.sender()
if sender.text() == "Submit":
entry = self.mod_num.text()
conn = sqlite3.connect("uprightsbeams.db")
c = conn.cursor()
c.execute("SELECT * FROM uprightbeam WHERE catalogNo=?", [entry])
results = c.fetchone()
print(results)
此代码还有更多内容,但这是我的程序中断的地方。几乎当按下按钮时,我想让它对我的uprightsbeams.db执行一个对catalogNo的查询,这样我就可以得到与之关联的行值。
我有这个catalogNo作为文本值存储在sqlite3中所以我认为它能够让它没问题,但我认为这在此之前就已经破了。我是否需要在方法之外连接到db?我尝试了很多不同的变化,我只是想不出来。
在这里发生这种情况时,看到没有错误代码是程序的代码。:
import sys
import os
import sqlite3
from PyQt5.QtWidgets import QTableWidget, QMainWindow, QApplication, QLabel, QTableWidgetItem, QWidget, QTableView
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5 import QtCore, QtWidgets, QtGui
class MyTable(QTableWidget):
def __init__(self):
super().__init__()
self.setGeometry(48, 0, 1080, 451)
self.setRowCount(34)
self.setColumnCount(14)
class Sheet(QtWidgets.QWidget):
def __init__(self):
super().__init__()
QMainWindow.setGeometry(self, 1, 30, 1918, 1050)
self.form_widget = MyTable()
self.row = 0
self.col = 0
col_headers = ['CATALOG NO.', 'DESCRIPTION', 'COLOR', 'QTY.', 'UN.', 'WGT.', 'Total Wgt.', 'List Price', 'Net', 'Total Net', 'Total List',"","Item Discount",""]
self.form_widget.setHorizontalHeaderLabels(col_headers)
self.form_widget.verticalHeader().setVisible(False)
#h_box1 Layout
self.cat_num_lab = QtWidgets.QLabel(self)
self.cat_num_lab.setText("Part Number:")
self.mod_num = QtWidgets.QLineEdit(self)
self.color_label = QtWidgets.QLabel(self)
self.color_label.setText("Color: ")
self.color_Entry = QtWidgets.QLineEdit(self)
self.qty_label = QtWidgets.QLabel(self)
self.qty_label.setText("QTY:")
self.qty_Entry = QtWidgets.QLineEdit(self)
self.sub_btn = QtWidgets.QPushButton("Submit")
self.sub_btn.clicked.connect(self.btn_clk)
#h_box2 Layout
self.cat_man_Entry = QtWidgets.QLabel(self)
self.cat_man_Entry.setText("Manual Entry")
self.man_part_num = QtWidgets.QLabel(self)
self.man_part_num.setText("Part Number:")
self.man_part_Entry = QtWidgets.QLineEdit(self)
self.man_disc = QtWidgets.QLabel(self)
self.man_disc.setText("Description: ")
self.man_disc_Entry = QtWidgets.QLineEdit(self)
self.man_color_label = QtWidgets.QLabel(self)
self.man_color_label.setText("Color: ")
self.man_color_Entry = QtWidgets.QLineEdit(self)
self.man_qty_label = QtWidgets.QLabel(self)
self.man_qty_label.setText("QTY:")
self.man_qty_Entry = QtWidgets.QLineEdit(self)
self.man_unit_meas = QtWidgets.QLabel(self)
self.man_unit_meas.setText("Unit Measure: ")
self.man_unit_meas_Entry = QtWidgets.QLineEdit(self)
self.man_wgt = QtWidgets.QLabel(self)
self.man_wgt.setText("WGT.: ")
self.man_wgt_Entry = QtWidgets.QLineEdit(self)
self.man_list_price = QtWidgets.QLabel(self)
self.man_list_price.setText("List Price: ")
self.man_list_price_Entry = QtWidgets.QLineEdit(self)
self.man_sub_btn = QtWidgets.QPushButton("Man. Submit")
self.man_sub_btn.clicked.connect(self.btn_clk)
#Layout
h_box1 = QtWidgets.QHBoxLayout()
h_box1.addWidget(self.cat_num_lab)
h_box1.addWidget(self.mod_num)
h_box1.addWidget(self.color_label)
h_box1.addWidget(self.color_Entry)
h_box1.addWidget(self.qty_label)
h_box1.addWidget(self.qty_Entry)
h_box1.addWidget(self.sub_btn)
h_box1.addStretch()
h_box2 = QtWidgets.QHBoxLayout()
h_box2.addWidget(self.cat_man_Entry)
h_box2.addWidget(self.man_part_num)
h_box2.addWidget(self.man_part_Entry)
h_box2.addWidget(self.man_disc)
h_box2.addWidget(self.man_disc_Entry)
h_box2.addWidget(self.man_color_label)
h_box2.addWidget(self.man_color_Entry)
h_box2.addWidget(self.man_qty_label)
h_box2.addWidget(self.man_qty_Entry)
h_box2.addWidget(self.man_unit_meas)
h_box2.addWidget(self.man_unit_meas_Entry)
h_box2.addWidget(self.man_wgt)
h_box2.addWidget(self.man_wgt_Entry)
h_box2.addWidget(self.man_list_price)
h_box2.addWidget(self.man_list_price_Entry)
h_box2.addWidget(self.man_sub_btn)
h_box2.addStretch()
h_box3 = QtWidgets.QHBoxLayout()
h_box3.addWidget(self.form_widget)
v_box = QtWidgets.QVBoxLayout()
v_box.addLayout(h_box1)
v_box.addLayout(h_box2)
v_box.addLayout(h_box3)
self.setLayout(v_box)
self.show()
def btn_clk(self):
sender = self.sender()
if sender.text() == "Submit":
entry = QTableWidgetItem(self.mod_num.text())
conn = sqlite3.connect("uprightsbeams.db")
c = conn.cursor()
c.execute("Select * FROM uprightbeam WHERE catalogNo=?", [entry])
results = c.fetchone()
print(results)
col = 0
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, entry)
col = 2
color = QTableWidgetItem(self.color_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, color)
col = 3
qty = QTableWidgetItem(self.qty_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, qty)
self.row += 1
self.mod_num.clear()
self.color_Entry.clear()
self.qty_Entry.clear()
else:
col = 0
cat_num = QTableWidgetItem(self.man_part_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, cat_num)
col = 1
man_disc = QTableWidgetItem(self.man_disc_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, man_disc)
col = 2
man_color = QTableWidgetItem(self.man_color_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, man_color)
col = 3
man_qty = QTableWidgetItem(self.man_qty_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, man_qty)
col = 4
man_unit = QTableWidgetItem(self.man_unit_meas_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, man_unit)
col = 5
man_wgt = QTableWidgetItem(self.man_wgt_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, man_wgt)
col = 7
man_price = QTableWidgetItem(self.man_list_price_Entry.text())
self.form_widget.setCurrentCell(self.row, col)
self.form_widget.setItem(self.row, col, man_price)
self.row += 1
self.man_part_Entry.clear()
self.man_disc_Entry.clear()
self.man_wgt_Entry.clear()
self.man_list_price_Entry.clear()
app = QApplication(sys.argv)
sheet = Sheet()
sys.exit(app.exec_())
更新
我从来不需要从控制台运行代码所以我从来没有这样做,总是通过IDLE运行。我通过控制台运行应用程序,这是追溯:
Traceback (most recent call last):
File "E:\srold.py", line 147, in btn_clk
c.execute("Select * FROM uprightbeam WHERE catalogNo=?", entry)
ValueError: parameters are of unsupported type
更新 当我尝试将QTableWidgetItem作为入口值传递时,我已经从ValueError开始了。我已将其更改为self.mod_num.text()中的QLineEdit文本。
现在我有了这个,我现在还不理解:
Traceback (most recent call last):
File "E:\srold.py", line 147, in btn_clk
c.execute("SELECT * FROM uprightbeam WHERE catalogNo=?", entry)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 12 supplied.
我不理解,因为在我的代码中我有:
c.execute("SELECT * FROM uprightbeam WHERE catalogNo=?", [entry])
我将条目设置为列表,我认为这会阻止不正确的绑定数问题。
答案 0 :(得分:1)
我已经开始工作了。
回答原始问题的事实是我试图将QTableWidgetItem的值传递给变量,当我可以直接使用框中的QLineEdit值时。我一路上也遇到了一些语法错误,但是我的btn_clk代码的完成顶部是:
sender = self.sender()
if sender.text() == "Submit":
entry = self.mod_num.text()
conn = sqlite3.connect("uprightsbeams.db")
c = conn.cursor()
c.execute("SELECT * FROM beamupright WHERE catalogNo=?", [entry])
results = c.fetchone()
print(results)
我现在可以继续这个项目了。