首页 > 解决方案 > TypeError:execute() 接受 2 到 3 个位置参数,但给出了 7 个

问题描述

我有以下代码,它抛出 TypeError: execute() 需要 2 到 3 个位置参数,但给出了 7 个。我不确定它是否正确,但它是:

result_time = cur.execute("SELECT appointment_id FROM appointments WHERE appointment_time =%s", [appointment_time], "AND appointment_date =%s", [appointment_date], "AND doctor_id =%s", [actual_doctor_id.get('doctor_id')])

因此,当满足所有要求时,我想要一个特定的约会 ID。

标签: pythonmysqlselectwhere

解决方案


cursor.execute获取 sql 和一个参数元组 - 你给了参数单独 - 因此你“过度填充”它并得到

TypeError:execute() 接受 2 到 3 个位置参数,但给出了 7 个

更改您的代码以包含 1 个 sql 语句和一个带参数的元组:

result_time = cur.execute(
    "SELECT appointment_id FROM appointments WHERE appointment_time = %s AND appointment_date = %s AND doctor_id = %s", 
    ( appointment_time, appointment_date, actual_doctor_id.get('doctor_id')) )          

它会起作用。

 cursor.execute( sql, ( param1, param2, ... ) )
 #                      this is all a tuple - hence the 2nd allowed param to execute.

请参阅 fe mysql-documentation或使用http://bobby-tables.com/python作为快速参考。


推荐阅读