mysql.connector.errors.ProgrammingError:1054(42S22):'where子句'中的未知列'X'

时间:2019-05-18 20:16:05

标签: python mysql-python

这是我的脚本:

if (cursor.execute("SELECT * FROM users WHERE (username =?  password = ?) VALUES(?,?)"(username,password))):

我正在安装在线系统,但是登录系统遇到问题。如何解决“ where子句中的未知列'x'”,或者您还有其他代码建议吗?

Traceback (most recent call last):
  File "C:/Users/artun/PycharmProjects/DENEMELER/Login System.py", line 21, in <module>
    loginsystem()
  File "C:/Users/artun/PycharmProjects/DENEMELER/Login System.py", line 15, in loginsystem
    if (cursor.execute("SELECT * FROM users WHERE username = %s AND password = %s"%(username,password))):
  File "C:\Users\artun\PycharmProjects\DENEMELER\venv\lib\site-packages\mysql\connector\cursor.py", line 569, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Users\artun\PycharmProjects\DENEMELER\venv\lib\site-packages\mysql\connector\connection.py", line 553, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "C:\Users\artun\PycharmProjects\DENEMELER\venv\lib\site-packages\mysql\connector\connection.py", line 442, in _handle_result
    raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'oziboran123' in 'where clause'

Process finished with exit code 1

我试图用这种方法来做,但是没有

FilesResource.ListRequest listRequest = service.Files.List();
listRequest.Fields = "*";

这是代码的输出,但是我希望代码的输出是“登录”或“失败”

2 个答案:

答案 0 :(得分:0)

考虑参数化而不是字符串插值,这涉及两步过程:准备语句和绑定参数的执行。下面使用了execute调用的两个参数:cursor.execute(query, params)。另外, user password 在MySQL中是reserved words,应使用反引号进行转义。

请注意,%s API的参数占位符mysql.connector不应与Python的模数字符串格式符号相混淆(顺便说一下,Python中的preferred string formatting method较少)首选str.format)。

# PREPARED STATEMENT
sql = """SELECT * FROM `users`
         WHERE `username` = %s AND `password` =%s
      """

# EXECUTE WITH PARAMS
cursor.execute(sql, (username, password))

答案 1 :(得分:0)

试试下面的代码:

 """SELECT * FROM `users` WHERE `username` '{}' AND `password` '{}'""".format(username,password))