ORM框架Peewee(五查)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/a6864657/article/details/100146064

1. 一般查询

user1 = User.select()

2. 单条查询

user2 = User.get(User.id == 883)
print user_info .dept_id, user_info .user_name

对比1和2个区别, 先获取他们的类型

print type(user1 ) ==> <class 'peewee.SelectQuery'>  
SelectQuery类型需要使用for循环逐条获取

Print type(user2) == > <class 'createDB.User'> 
本身就是一个实例的对象可以直接获取它的属性

3. 查询部分字段

user3= User.select(User.user_name)

4. 有条件查询

user4 = User.select().where(User.id == 12)

5. 随机查询(需要先引入fn)

from peewee import fn

user5 = User.select().order_by(fn.Random()).limit(2)

6. 排序查询

  • 正序asc

     user6 = User.select().order_by(User.dept_id.asc())
    
  • 倒序desc

     user6 = User.select().order_by(User.dept_id.desc())
    

7. Not in组合查询

现有学生信息表student_info
学生姓名student_name
学号student_no
学生成绩表score_table
学号student_no
分数score

st7 = StudentsInfo.select(StudentsInfo.student_no).where(StudentsInfo.student_no > 880)
sc = StudentsScore.select().where(StudentsScore.student_no.not_in(st7))

8. 模糊查询

比如想要查询学生名字包含’ba’的学生以及学号
%符号就相当于sql里的like

st8 = StudentsInfo.select().where(StudentsInfo.student_name % '%ba%')
for i in st8:
    print i.student_no,i.student_name

Peewee做一个.where(somecolumn == None / Null / Empty)
Message.select().where(Message.somecolumn.is_null(False))

基本语法
peewee(我喜欢这个)

Peewee 使用

Peewee使用之事务

peewee官方文档

猜你喜欢

转载自blog.csdn.net/a6864657/article/details/100146064