mysql/python语法错误

5vf7fwbs  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(301)

我经常在用python编写的sql查询中遇到错误。
以下是相关代码:

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

错误信息如下:

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

有人能发现错误吗?我已经改变了很多次,包括在变量中包含部分sql查询,但是我收到了相同的错误。

2guxujil

2guxujil1#

您的查询不正确,因为您不能有2个where子句
你只能传递一个查询字符串
所以要做到:

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

查询的参数需要作为元组传递

相关问题