Python的ORM框架Peewee使用入门(三)
来源:互联网 发布:淘宝的放心淘和运费险 编辑:程序博客网 时间:2024/06/08 05:01
我们不要认为这颗行星上的一切都是理所当然的
检索数据
from datetime import datefrom peewee import *db = SqliteDatabase('people.db')class Person(Model): name = CharField() birthday = DateField() is_relative = BooleanField() class Meta: database = db #用了“people.db”数据库class Pet(Model): owner = ForeignKeyField(Person, related_name='pets') name = CharField() animal_type = CharField() class Meta: database = db #用了“people.db”数据库"""-------------------------------------------------------------------------------------------------------"""#获取单个数据记录grandma = Person.select().where(Person.name == 'Grandma L.').get()"""同上"""grandma = Person.get(Person.name == 'Grandma L.')#获取数据列表"""for person in Person.select(): print("人名:",person.name, person.is_relative)query = Pet.select().where(Pet.animal_type == 'cat')for pet in query: print("宠物名:",pet.name, "主人名:",pet.owner.name)"""#连接查询"""query = (Pet.select(Pet, Person) .join(Person) .where(Pet.animal_type == 'cat'))for pet in query: print(pet.name, pet.owner.name)"""#让我们获取Bob拥有的所有宠物for pet in Pet.select().join(Person).where(Person.name == 'Bob'): print(pet.name)"""同上"""uncle_bob = Person(name='Bob', birthday=date(1960, 1, 15), is_relative=True)for pet in Pet.select().where(Pet.owner == uncle_bob).order_by(Pet.name): print(pet.name)#日期排序for person in Person.select().order_by(Person.birthday.desc()): print(person.name, person.birthday)"""-------------------------------------------------------------------------------------------------------"""for person in Person.select(): print(person.name, person.pets.count(), 'pets') for pet in person.pets: print (' ', pet.name, pet.animal_type)"""-------------------------------------------------------------------------------------------------------"""#日期条件查询d1940 = date(1940, 1, 1)d1960 = date(1960, 1, 1)#查询生日大于1960年,小于1940年query = (Person.select() .where((Person.birthday < d1940) | (Person.birthday > d1960)))for person in query: print(person.name, person.birthday)#查询生日在1940年与1960年 之间的人query = (Person .select() .where((Person.birthday > d1940) & (Person.birthday < d1960)))for person in query: print(person.name, person.birthday)"""------------------------------------------------------"""#查询人名,g开头的expression = (fn.Lower(fn.Substr(Person.name, 1, 1)) == 'g')for person in Person.select().where(expression): print(person.name)"""连接数据库连接"""db.close()
0 0
- Python的ORM框架Peewee使用入门(三)
- Python的ORM框架Peewee使用入门(一)
- Python的ORM框架Peewee使用入门(二)
- Python ORM框架之 Peewee入门
- 推荐python的ORM框架peewee(转)
- python ORM 模块peewee(三): Model的建立
- Python的ORM框架SQLAlchemy使用入门(三)
- python ORM 模块peewee(二): 数据库使用的基本流程
- Python中的ORM使用之peewee
- peewee 一个轻量级的ORM (三)
- python轻量级ORM---peewee
- 轻量级的ORM框架Peewee访问sqlite数据库
- Python的ORM框架SQLAlchemy使用入门(一)
- peewee 一个轻量级的ORM
- python轻量级ORM---peewee之API
- peewee 一个轻量级的ORM(一)
- peewee 一个轻量级的ORM(二)
- peewee 一个轻量级的ORM (四)
- Cocos2d-x-3.11生成安卓预编译库报错的解决办法
- 安卓小日记练习(9)序列化对象List,通过文件读取在活动间传递数据
- 博雅张伟:死磕腾讯之后穷屌丝如何变身大土豪?
- Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
- 选股系统
- Python的ORM框架Peewee使用入门(三)
- 在centos 7上自动安装starUML,且破解之。
- 数组---求数组组成的集合的所有子集
- 在Mac OS X 10.9.1(x86, 64-bit)上配置MySQL数据库
- cmake常用工程示例大集合
- Days13 SQLiteDatabase&SQLiteOpenHelper&SimpleCursorAdapter
- C语言中printf格式详解
- 2016青岛网赛1001
- Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))