【问题标题】:How to use DATE_FORMAT in the where clause using mysql.connector ? It produces Not enough parameters for the SQL statement error [duplicate]如何使用 mysql.connector 在 where 子句中使用 DATE_FORMAT ?它为 SQL 语句错误产生没有足够的参数 [重复]
【发布时间】:2021-05-28 19:34:13
【问题描述】:
_SQL = """SELECT u.first_name, u.last_name, 
                 DATE_FORMAT(a.arrival_time, '%H:%i:%s') AS arrival_time, 
                 DATE_FORMAT(a.leave_time, '%H:%i:%s') AS leave_time 
          FROM attendance a JOIN users u ON (a.user_id = u.user_id) 
          WHERE DATE_FORMAT(a.arrival_time, '%Y-%m-%d') = %s"""

cursor.execute(_SQL, (str(datetime.date.today()), ))

您好,执行此查询会产生错误: mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement

在我看来,这个查询似乎是正确的。任何人都可以在查询中有什么问题吗?帮助将不胜感激

【问题讨论】:

  • 你可以check this out.
  • 在您的格式字符串中,使用%%s 转义@Georg 答案中所建议的%%S

标签: mysql python-3.x mariadb mysql-python


【解决方案1】:

这是 MySQL Connector Python 的解析器中的一个错误,它试图用参数替换 date_format 函数中的%s。由于这个错误,它总共需要 3 个参数,而参数元组中只指定了一个参数。

作为一种解决方法,您可以在 date_format 函数中以大写形式指定格式参数秒数,这应该可以解决问题: DATE_FORMAT(time, '%H:%i:%S')

【讨论】:

    猜你喜欢
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多