【问题标题】:Compare python time with MySQL time format比较python时间和MySQL时间格式
【发布时间】:2017-12-16 16:48:15
【问题描述】:

我在 python 中有这个 if 语句:

if acceso < dateString < spento:
  [SOME CODE]
else:
  [OTHER CODE]

地点:

dateString = strftime('%H:%M:%S')

而acceso 和spendo 是MySQL 查询的结果:

orario = ("SELECT * FROM orari WHERE attivo = 1")
        cur.execute(orario)
        row = cur.fetchone()
        acceso = datetime.strftime(row[1], '%H:%M:%S')
        spento = datetime.strftime(row[2], '%H:%M:%S')

如果我手动将acceso 和spendo 设置为06:00:00 和22:00:00 这样的值,则脚本运行良好。我尝试了一些方法将 row[1] 和 row[2] 转换为 datetime 对象,这给了我这个错误:

TypeError:描述符“strftime”需要一个“datetime.date”对象,但收到一个“datetime.timedelta”

【问题讨论】:

    标签: python mysql date


    【解决方案1】:

    我采取了最简单的方法:将 MySQL 字段类型从 time 更改为 text。不是最好的,但就我而言,我不需要更准确的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多