繁体   English   中英

类型错误:execute() 需要 2 到 3 个位置参数,但给出了 7 个

[英]TypeError: execute() takes from 2 to 3 positional arguments but 7 were given

我有以下代码,它抛出 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。

cursor.execute接受 sql 和一个 params 元组 - 你单独给了 params - 因此你“过度填充”它并得到

类型错误: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作为快速参考。

暂无
暂无

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

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