简体   繁体   English

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

[英]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.我有以下代码,它抛出 TypeError:execute() 需要 2 到 3 个位置参数,但给出了 7 个。 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.因此,当满足所有要求时,我想要一个特定的约会 ID。

cursor.execute takes the sql and a tuple of params - you gave the params singled-ly - hence you "overstuffed" it and get cursor.execute接受 sql 和一个 params 元组 - 你单独给了 params - 因此你“过度填充”它并得到

TypeError: execute() takes from 2 to 3 positional arguments but 7 were given类型错误:execute() 需要 2 到 3 个位置参数,但给出了 7 个

Change your code to contain 1 sql statement and one tuple with params:更改您的代码以包含 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')) )          

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.请参阅 fe mysql-documentation或使用http://bobby-tables.com/python作为快速参考。

暂无
暂无

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

相关问题 Python/MySQL TypeError:execute() 需要 2 到 4 个位置参数,但给出了 5 个 - Python/MySQL TypeError: execute() takes from 2 to 4 positional arguments but 5 were given TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given - TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given Money 和 TypeError:__init__() 需要 1 到 2 个位置参数,但给出了 3 个 - Money and TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given TypeError:send()接受1到2个位置参数,但给出了3个 - TypeError: send() takes from 1 to 2 positional arguments but 3 were given 类型错误:read_csv() 需要 0 到 1 个位置参数,但给出了 2 个 - TypeError: read_csv() takes from 0 to 1 positional arguments but 2 were given 类型错误:raw_input() 需要 1 到 2 个位置参数,但给出了 4 个 - TypeError: raw_input() takes from 1 to 2 positional arguments but 4 were given Django TypeError:url()需要2到4个位置参数,但是给出了16个 - Django TypeError: url() takes from 2 to 4 positional arguments but 16 were given 继承 TypeError: __init__() 接受 1 到 2 个位置参数,但给出了 8 个 - inheritance TypeError: __init__() takes from 1 to 2 positional arguments but 8 were given TypeError: append() 从 2 到 5 个位置 arguments 但给出了 8 个 - TypeError: append() takes from 2 to 5 positional arguments but 8 were given TypeError: with_column() 从 3 到 4 个位置 arguments 但给出了 5 个(python) - TypeError: with_column() takes from 3 to 4 positional arguments but 5 were given (python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM