简体   繁体   English

MySQL / Python语法错误

[英]MySQL/Python Syntax Error

I keep getting a error with a SQL query that is written in python. 我用python编写的SQL查询不断出现错误。

Here is the code in question: 这是有问题的代码:

else:
    else_query = "SELECT count(*) FROM PARKING_SPOTS WHERE OCCUPANCY = %s"
    cursor.execute(else_query, (occupancy,)
                   " AND WHERE LOCATION = %s", (location,))

Here's the error message: 这是错误消息:

File "exp1", line 116
    " AND WHERE LOCATION = %s", (location,))
                             ^
SyntaxError: invalid syntax

Can anyone spot the error ? 谁能发现错误? I've changed things around several times, including containing part of the SQL query in a variable, yet I receive the same error. 我已经进行了数次更改,包括将SQL查询的一部分包含在一个变量中,但是我收到了同样的错误。

  • your query is incorrect because you can't have 2 WHERE clauses 您的查询不正确,因为您不能有2个WHERE子句
  • you can only pass one querystring 您只能传递一个查询字符串

so make that: 因此,使:

    else_query = """SELECT count(*) FROM PARKING_SPOTS WHERE OCCUPANCY = %s
                    AND LOCATION = %s
                 """
    cursor.execute(else_query, (occupancy, location))

parameters for the query need to be passed as a tuple 查询的参数需要作为元组传递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM