python - TypeError:execute() 接受 2 到 3 个位置参数,但给出了 7 个
问题描述
我有以下代码,它抛出 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 和一个参数元组 - 你给了参数单独 - 因此你“过度填充”它并得到
TypeError: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作为快速参考。
推荐阅读
- node.js - 限制用户通过 GET 请求访问 admin 和 _id 详细信息
- spring-boot - 找不到属性 SpringBoot JPA
- python - 我可以用 groupy 和 pandas 计算百分比吗
- angular - 我怎样才能把这个物体以角度的方式放在材料表中
- docker - 在 docker4drupal 中安装 npm 和 gulp
- algorithm - 具有两个自变量的嵌套 for 循环的时间复杂度
- javascript - 为 HTML 视频添加剩余时间
- python - 有人可以解释一下这个 leetcode 字符串操作问题吗?
- pseudocode - 在 Marie.js 中,我将如何为以下伪代码执行 if 语句和循环?
- php - firebase php sdk 不支持多过滤数据