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

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')])

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

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

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

更改您的代码以包含 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.

参见 f.e。 mysql-documentation or use http://bobby-tables.com/python 作为快速参考。