【问题标题】:Using Peewee ORM with MySQL date math将 Peewee ORM 与 MySQL 日期数学一起使用
【发布时间】:2018-09-09 11:31:32
【问题描述】:

我正在尝试将使用 MySQLdb 的应用程序转换为使用 Peewee。大部分的 SELECT 和 INSERT 都没有问题,但是一类查询让我很困惑。

原代码包含:

sql = "SELECT * FROM {tbl} WHERE tail='{tail}' AND flight="+\
    "'{flight}' AND dest='{dest}' AND orig='{orig}' AND "+\
    "oooi='{oooi}' AND report_time > ('{time}' - INTERVAL 2 HOUR) "+\
    "AND report_time < ('{time}' + INTERVAL 2 HOUR)"
cmd = sql.format(tbl = self.table, tail=tail, flight=flight, dest=dest,
     orig=orig, time = report_time, oooi=oooi)

c.execute(cmd)
return c.fetchone()

尝试重写它以使用我想出的 Peewee:

oooi_rec = Oooi_rec.select().where(Oooi_rec.tail == self.tail, 
    Oooi_rec.flight == self.flight, 
    Oooi_rec.dest == self.dest, Oooi_rec.orig == self.orig, 
    Oooi_rec.oooi=self.oooi,
    Oooi_rec.report_time.between(low, high))

替换“低”和“高”的位现在让我感到困惑。我想弄清楚如何使用 Peewee 的 fn() 但它进展缓慢。

【问题讨论】:

    标签: mysql time peewee


    【解决方案1】:

    也许:

    low - SQL('interval 2 hour')
    

    另外,你以前的方式有这么多的 sql 注入......是的。

    【讨论】:

      猜你喜欢
      • 2012-01-05
      • 1970-01-01
      • 2017-03-26
      • 2013-08-03
      • 2022-01-12
      • 2011-10-22
      • 2021-08-31
      • 2019-01-09
      • 1970-01-01
      相关资源
      最近更新 更多