首页 > 解决方案 > 将 Peewee ORM 与 MySQL 日期数学一起使用

问题描述

我正在尝试将使用 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() 但它进展缓慢。

标签: mysqltimepeewee

解决方案


也许:

low - SQL('interval 2 hour')

此外,以您以前的方式进行了如此多的 sql 注入......是的。


推荐阅读