无法从SQLite for Python中的查询中提取多个值

时间:2017-02-26 04:34:22

标签: python sqlite

所以,我试图从Python 3.5中的SQlite3查询中获取单行数据的多个值。我可以为我的一组数据做到这一点而没有任何问题。当我尝试从已保存为结果的返回列表中提取不同的值时,我开始收到以下错误:

Traceback (most recent call last):
  File "E:\speedrack.py", line 183, in btn_clk
    c.execute("SELECT * FROM beamupright WHERE catalogNo=?", [entry])
sqlite3.OperationalError: no such table: beamupright

以下代码将光盘中的值放入#Description。

中指定的单元格没有问题
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 beamupright WHERE catalogNo=?", [entry])
        results = c.fetchone()
        disc = results[2]
##            unit = results[5]
##            wgt = results[3]
##            l_price = results[4]
        print(results)
        print(disc)
##            print(unit)
##            print(wgt)
##            print(l_price)
    #Catalot No.
        col = 0
        tab_entry = QTableWidgetItem(self.mod_num.text())
        self.form_widget.setCurrentCell(self.row, col)
        self.form_widget.setItem(self.row, col, tab_entry)
    #Discription
        col = 1
        tab_disc = QTableWidgetItem(disc)
        self.form_widget.setCurrentCell(self.row, col)
        self.form_widget.setItem(self.row, col, tab_disc)

当我去做以下事情时:

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 beamupright WHERE catalogNo=?", [entry])
        results = c.fetchone()
        disc = results[2]
        unit = results[5]
##            wgt = results[3]
##            l_price = results[4]
        print(results)
        print(disc)
        print(unit)
##            print(wgt)
##            print(l_price)
    #Catalot No.
        col = 0
        tab_entry = QTableWidgetItem(self.mod_num.text())
        self.form_widget.setCurrentCell(self.row, col)
        self.form_widget.setItem(self.row, col, tab_entry)
    #Discription
        col = 1
        tab_disc = QTableWidgetItem(disc)
        self.form_widget.setCurrentCell(self.row, col)
        self.form_widget.setItem(self.row, col, tab_disc)
    #Color
        col = 2
        color = QTableWidgetItem(self.color_Entry.currentText())
        self.form_widget.setCurrentCell(self.row, col)
        self.form_widget.setItem(self.row, col, color)
    #QTY
        col = 3
        qty = QTableWidgetItem(self.qty_Entry.text())
        self.form_widget.setCurrentCell(self.row, col)
        self.form_widget.setItem(self.row, col, qty)
    #place holder for UN. cell
        col = 4
        un_tab = QTableWidgetItem(unit)
        self.form_widget.setCurrnetCell(self.row, col)
        self.form_widget.setItem(self.row, col, un_tab)

是我收到错误的时候:

Traceback (most recent call last):
      File "E:\speedrack.py", line 183, in btn_clk
        c.execute("SELECT * FROM beamupright WHERE catalogNo=?", [entry])
    sqlite3.OperationalError: no such table: beamupright

造成这种情况的错误是什么?

2 个答案:

答案 0 :(得分:0)

错误消息显示您打开的数据库为空。

由于您在某个随机目录中打开数据库,因此您可能会得到一个空数据库。

始终使用数据库文件的完整路径。

答案 1 :(得分:-1)

它奏效了,然后失败了。

原因:您从未关闭连接并刷新光标,导致表格被锁定且无法访问。

更改以下代码部分:

c = conn.cursor()
c.execute("SELECT * FROM beamupright WHERE catalogNo=?", [entry])
results = c.fetchone()

为:

with conn:
    c = conn.cursor()
    c.execute("SELECT * FROM beamupright WHERE catalogNo=?", [entry])
    results = c.fetchone()

这将在查询运行完毕后自动释放连接。