mysql - 将 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() 但它进展缓慢。
解决方案
也许:
low - SQL('interval 2 hour')
此外,以您以前的方式进行了如此多的 sql 注入......是的。
推荐阅读
- javascript - 如何在 JavaScript 中转换和重新排序数组?
- reactjs - 如何在没有标签的表单控件或表单控件标签内编写复选框
- python - Python 不接受大数?
- python - 如何设置刻度标签旋转并添加条形注释
- flutter - 在 Firestore 中存储参考字符串
- javascript - Redux preloadedState 导致默认减速器状态的覆盖(以及随后的未定义错误)?
- google-cloud-platform - gcp vm worker 默认网关
- javascript - 打字稿数组语义错误 TS2532:对象可能是“未定义”
- c# - 如何将 IConfiguration 注入托管工作者服务的程序类中?
- node.js - 我如何使用 socket.io-client 在不同的角度显示发送方和接收方消息