简体   繁体   中英

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

I have the following code and it is throwing the TypeError: execute() takes from 2 to 3 positional arguments but 7 were given. I'm not sure if it's right but here it is:

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

So I want a particular appointment_id when all requirements are met.

cursor.execute takes the sql and a tuple of params - you gave the params singled-ly - hence you "overstuffed" it and get

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

Change your code to contain 1 sql statement and one tuple with 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')) )          

and it will work.

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

See fe mysql-documentation or use http://bobby-tables.com/python as a quick reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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