将变量传递给python中的sql查询时出错

时间:2019-12-10 08:47:35

标签: python mysql sql mysql-connector-python

我正在python中使用sql查询。我想在查询中传递一些值作为参数/变量。我很少看到相关问题。我正在关注这些问题,但是我不确定我要去哪里错。我的代码

Label myLabel = new Label {Name = "myLabel, "Text = "+", Font = new Font("Microsoft Sans Serif", 14, FontStyle.Bold), ForeColor = System.Drawing.Color.ForestGreen, AutoSize = true, Enabled = false};

出现错误:

  

SQL语句中未使用所有参数

我也尝试过%s,该方法也出现错误。有人可以帮助我正确地传递变量吗?

1 个答案:

答案 0 :(得分:1)

使用%s参数标记,以便您的查询成为

>>> q="""SELECT t1.deviceId, t1.date, t1.vavId, t1.timestamp, t1.nvo_airflow , t1.nvo_air_damper_position , t1.nvo_temperature_sensor_pps
                        FROM
                            (SELECT deviceId, date, nvo_airflow, nvo_air_damper_position, nvo_temperature_sensor_pps, vavId, timestamp, counter from vavData where date='2019-12-10' and floor=%s) t1
                        INNER JOIN
                             (SELECT date,max(timestamp) as timestamp,vavId from vavData where date='2019-12-10' and floor=%s group by vavId) t2
                        ON (t1.timestamp = t2.timestamp) """

>>> cursor.execute(q,('xxxx', 'xxxx'))

现在只需按预期运行python 3.7mysql.connector.version.VERSION (8, 0, 18, '', 1)即可。

选中this answer also