如何在SQL查询中使用多个Python变量

时间:2019-03-30 23:15:43

标签: python mysql sql variables

目前,我正在尝试使用带有Python变量的mySQL数据库查询。我了解在查询中使用变量的约定如下:

curr.execute("INSERT INTO table VALUES (%s, %s, %s)", (var1, var2, var3))

但是,我要执行的查询如下:

SELECT * FROM table1
WHERE City = (cityName)
AND AdmissionDate BETWEEN (startDate) AND (endDate)

我不确定变量将如何格式化,无论它们是否都必须在1元组中,还是我可以像在printf语句中那样格式化它,所以我不确定下面哪个是正确的:

curr.execute("SELECT * FROM table1
              WHERE City = (%s)
              AND AdmissionDate BETWEEN (%s) AND (%s)",(cityName, startDate, endDate))


curr.execute("SELECT * FROM table1
              WHERE City = (%s)
              AND AdmissionDate BETWEEN (%s) AND (%s)", (cityName), (startDate), (endDate))

任何提示将不胜感激。预先感谢。

0 个答案:

没有答案