peewee

Posted escapist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了peewee相关的知识,希望对你有一定的参考价值。

query = Facility.select()   等价于SQL   SELECT * FROM facilities
Facility.select(Facility.name, Facility.membercost)  等价于  SELECT name, membercost FROM facilities;
Facility.select().where(Facility.membercost > 0) 等价于 SELECT * FROM facilities WHERE membercost > 0
Facility.select().where(Facility.name.contains(‘tennis‘)) SELECT * FROM facilities WHERE name ILIKE ‘%tennis%‘;

 或者   Facility.select().where(Facility.name ** ‘%tennis%‘)

 

query = Facility.select().where(Facility.facid.in_([1, 5]))   in操作

 

SELECT name,
CASE WHEN monthlymaintenance > 100 THEN ‘expensive‘ ELSE ‘cheap‘ END
FROM facilities;

 对应ORM语句

cost = Case(None, [(Facility.monthlymaintenance > 100, ‘expensive‘)], ‘cheap‘)
query = Facility.select(Facility.name, cost.alias(‘cost‘))

 

比较日期

SELECT memid, surname, firstname, joindate FROM members
WHERE joindate >= ‘2012-09-01‘;
Member.select(Member.memid, Member.surname, Member.firstname, Member.joindate).where(Member.joindate >= datetime.date(2012, 9, 1))

 

SELECT DISTINCT surname FROM members ORDER BY surname LIMIT 10;
Member.select(Member.surname).order_by(Member.surname).limit(10).distinct()

 UNION 操作  把查询到的数据 合并 到一起  前提:列数相同

SELECT surname FROM members UNION SELECT name FROM facilities;
lhs = Member.select(Member.surname)
rhs = Facility.select(Facility.name)
query = lhs | rhs
  • |   代表   UNION        相同的只显示一次
  • +   代表   UNION ALL    都显示
  • &   代表   INTERSECT    只显示相同的
  •  代表   EXCEPT       

 

SELECT MAX(join_date) FROM members;
query = Member.select(fn.MAX(Member.joindate))

 

以上是关于peewee的主要内容,如果未能解决你的问题,请参考以下文章

使用Peewee库进行批量更新

如何使用 Peewee 查询几个相似的数据库?

用普通的peewee模块替换flask_peewee.db?

peewee 和 peewee-async:为啥异步速度较慢

python-轻量级ORM库peewee的使用

python-轻量级ORM库peewee的使用