TypeError:execute()需要2到3个位置参数,但是给出了7个

问题描述 投票:3回答:1

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

所以当满足所有要求时,我想要一个特定的appointment_id。

python mysql select where
1个回答
3
投票

cursor.execute采用了sql和一个params的元组 - 你单独给了params - 因此你“填充”了它并获得

TypeError:execute()需要2到3个位置参数,但是给出了7个

更改您的代码包含1个sql语句和一个带有params的元组:

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( slq, ( param1, param2, ... ) )
 #                      this is all a tuple - hence the 2nd allowed param to execute.

见f.e. myslq-documentation或使用http://bobby-tables.com/python作为快速参考。

© www.soinside.com 2019 - 2024. All rights reserved.