【问题标题】:Unknown Column exception in Mysql 1054Mysql 1054 中的未知列异常
【发布时间】:2016-11-13 17:44:31
【问题描述】:

我尝试了所有可能的方法。我在堆栈中包含了字符串的 backQuotes,但没有任何效果。它像往常一样重复错误。 我还尝试了一些在其他 python 文件中工作的查询,但它仍然显示相同。我也尝试使用不带连字符的字符串进行查询,即使它不起作用。我在这里找不到问题所在。

import MySQLdb
    import sys

    from PyQt4 import QtCore, QtGui, uic

    qtCreatorFile = "Studisplay.ui"  # Enter file here.

    Ui_MainWindow1, QtBaseClass = uic.loadUiType(qtCreatorFile)

    class stuDisplay(QtGui.QMainWindow, Ui_MainWindow1,QtGui.QTableWidget):
        def __init__(self,ID):
            #super(stuDisplay, self).__init__(parent)
            QtGui.QMainWindow.__init__(self)
            Ui_MainWindow1.__init__(self)
            QtGui.QWidget.__init__(self)
            self.setupUi(self)
            obj = MySQLdb.connect("localhost", "root", "1234567", "python")

            #The value of ID here is 14-VEC-244 I also tried `14-VEC-244` but did not work
            sql = 'SELECT MEMname FROM Borrowed WHERE MemberID ='+ str(ID)         

            cursor = obj.cursor()
            cursor.execute(sql)
            name=cursor.fetchone()
            print name

我收到此错误:

Traceback(最近一次调用最后一次):文件 “/home/gautham/PycharmProjects/LIBALERT/Login.py”,第 105 行,在 pushButton_clicked self.call = StuSecond.stuDisplay(StuID) 文件“/home/gautham/PycharmProjects/LIBALERT/StuSecond.py”,第 22 行,在 初始化 cursor.execute(sql) 文件“/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py”,第 226 行,在 执行 self.errorhandler(self, exc, value) 文件“/usr/lib/python2.7/dist-packages/MySQLdb/connections.py”,第 36 行,在 默认错误处理程序 提高错误值 _mysql_exceptions.OperationalError: (1054, "'where 子句'中的未知列 'VEC'")

【问题讨论】:

    标签: mysql pyqt


    【解决方案1】:

    您将一个字符串传递到您的WHERE 子句中,因此它必须在传递给数据库的查询字符串中被引用,如下所示:

    sql = "SELECT MEMname FROM Borrowed WHERE MemberID = '" + str(ID) + "'"
    

    这样完成的字符串看起来像

    sql = "SELECT MEMname FROM Borrowed WHERE MemberID = '14-VEC-244'"

    (请注意,单引号是“向前”引号,而不是反引号。)

    这对于准备好的语句也是一个很好的应用;不幸的是,我对 pyqt 不熟悉,因此无法在那里为您提供建议。

    【讨论】:

      猜你喜欢
      • 2012-10-08
      • 1970-01-01
      • 2020-11-22
      • 2016-04-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      相关资源
      最近更新 更多