将Peewee ORM与MySQL日期数学一起使用

时间:2018-09-07 20:52:46

标签: mysql time peewee

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

1 个答案:

答案 0 :(得分:0)

也许:

low - SQL('interval 2 hour')

此外,您以前使用过的方式还需要进行大量的SQL注入...网格。