【问题标题】:python with MySQLdb user input datepython 与 MySQLdb 用户输入日期
【发布时间】:2013-12-22 17:21:46
【问题描述】:

我在使用时不断从数据库中返回空白数据:

id = str(raw_input("Enter Date to run such as YYYY-MM-DD:  "))
cur.execute("SELECT * FROM appointment a where a.AptDateTime Like %s limit 0,100",id)

我认为查询时会发送一个额外的反引号,但不确定-

布拉德

【问题讨论】:

  • 不应该是appointment as a吗?
  • 没有。这是列别名。
  • 尝试修剪您的输入。如果这不起作用,请发布您的输入和您应该返回的记录之一。

标签: python mysql-python


【解决方案1】:

我在这个答案中假设AptDateTimeDATETIME 列。

不要用LIKE查询:

>>> cur.execute('SELECT * FROM apt WHERE aptdatetime LIKE %s', '2013-12-22')
0L

使用DATE() function提取日期并使用=查询:

>>> cur.execute('SELECT * FROM apt WHERE DATE(aptdatetime) = %s', '2013-12-22')
1L

【讨论】:

  • 感谢 Gareth!,根据您的建议,我能够得到正确的记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多