首页 文章

Mysql 1054中的未知列异常

提问于
浏览
0

我尝试了所有可能的手段 . 我在字符串中包含了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行,在init 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行,在defaulterrorhandler中引发errorvalue _mysql_exceptions.OperationalError:(1054,“where子句中的未知列'VEC' ““)

1 回答

  • 2

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

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

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

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

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

    这对于准备好的陈述也是一个很好的应用;不幸的是我不熟悉pyqt,所以不能在那里建议你 .

相关问题