Python中peewee模块

Python中peewee模块,有须要的朋友能够参考下。python


前言关于ORM框架:linux

简介:数据库

对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不一样类型系统的数据之间的转换。从效果上说,它实际上是建立了一个可在编程语言里使用的“虚拟对象数据库”。express

对象关系映射(Object-Relational Mapping)提供了概念性的、易于理解的模型化数据的方法。ORM方法论基于三个核心原则: 简单:以最基本的形式建模数据。 传达性:数据库结构被任何人都能理解的语言文档化。 精确性:基于数据模型建立正确标准化了的结构。 典型地,建模者经过收集来自那些熟悉应用程序但不熟练的数据建模者的人的信息开发信息模型。建模者必须可以用非技术企业专家能够理解的术语在概念层次上与数据结构进行通信。建模者也必须能以简单的单元分析信息,对样本数据进行处理。ORM专门被设计为改进这种联系。编程

ORM优点:windows

1.隐藏了数据访问细节,“封闭”的通用数据库交互,ORM的核心。他使得咱们的通用数据库交互变得简单易行,而且彻底不用考虑该死的SQL语句。快速开发,由此而来。数据结构

2.在ORM年表的史前时代,咱们须要将咱们的对象模型转化为一条一条的SQL语句,经过直连或是DB helper在关系数据库构造咱们的数据库体系。而如今,基本上全部的ORM框架都提供了经过对象模型构造关系数据库结构的功能。app

peewee模块(轻量级python中的ORM)1.安装peewee模块:框架

(Ps:首先安装好pip,才能够执行如下命令安装)
linux :
编程语言

sudo pip install peewee
windows:cmd下输入:

pip install peewee

2.peewee代码实例:数据库和表模型的准备:

# /usr/bin/python
# encoding:utf-8
from peewee import *from datetime
import date
# 新建数据库
dbdb = SqliteDatabase('people.db')
#表格模型
Person:这是一个Model的概念
class Person(Model):   
#CharField 为抽象数据类型 至关于 varchar    
name = CharField()   
#DateField 至关于 date   
birthday = DateField()  
#BooleanField 至关于 bool   
is_relative = BooleanField()   
# 所用数据库为db   
class Meta:database = db
#表格模型 Pet
class Pet(Model):   
#外链接的声明(和Person关联)   
owner = ForeignKeyField(Person, related_name='pets')  
name = CharField()   
animal_type = CharField()   
class Meta:database = db
#链接数据库
dbdb.connect()
在db中建表Person和Pet:
db.create_tables([Person, Pet])
Person 和 Pet表数据操做:
# Storing
Datauncle_bob = Person(name='Bob', birthday=date(1967, 1, 28), is_relative=True)
uncle_bob.save()
grandma = Person.create(name='Grandma', birthday=date(1935, 3, 1), is_relative=True)
herb = Person.create(name='Herb', birthday=date(1950, 5, 1), is_relative=False)
grandma.name = 'Marry'grandma.save()
bob_kitty = Pet.create(owner=uncle_bob, name='Kitty', animal_type='cat')
herb_fido = Pet.create(owner=herb, name='Fido', animal_type='dog')
herb_mittens = Pet.create(owner=herb, name='Mittens', animal_type='cat')
herb_mittens_jr = Pet.create(owner=herb, name='Mittens Jr', animal_type='cat')
# return the value of delete_instance() is the number of rows removed form the database
# delete
Dataherb_mittens.delete_instance() 
# he had a great life
# Modify
Dataherb_fido.owner = uncle_bobherb_fido.save()
bob_fido = herb_fido 
# rename our variable for clarityPerson,Pet—>Select 操做:
# Retrieving Data
# 查询名字为Marry的person
grandma = Person.select().where(Person.name == 'Marry').get()
#列出Person表中全部的person
for person in Person.select():      
print person.name, person.is_relative
#查询Pet表中animal_type为cat的全部pet
query = (Pet .select(Pet, Person) .join(Person) .where(Pet.animal_type == 'cat'))  
for pet in query:        
print pet.name, pet.owner.name
#查询Pet表中主人名为Bob的全部pet
for pet in Pet.select().join(Person).where(Person.name == 'Bob'):        
print pet.name
#查询Pet表中person为uncle_bob的全部pet
for pet in Pet.select().where(Pet.owner == uncle_bob):        
print pet.name
#查询Pet表中person为uncle_bob结果按pet名排列
for pet in Pet.select().where(Pet.owner == uncle_bob).order_by(Pet.name):             
print pet.name #将Person表中的person按生日降序查询
for person in Person.select().order_by(Person.birthday.desc()):        
print person.name, person.birthday
#查询Person表中person所拥有的pet数量及名字和类型
for person in Person.select():        
print person.name, person.pets.count(), 'pets'    
for pet in person.pets:     print '      ', pet.name, pet.animal_type
#查询Person表中生日小于1940或大于1960的person
d1940 = date(1940, 1, 1)
d1960 = date(1960, 1, 1)
query = (Person .select() .where((Person.birthday < d1940) | (Person.birthday > d1960)))
#查询Person表中生日在1940和1960之间的person
for person in query:        
print person.name, person.birthday
query = (Person .select() .where((Person.birthday > d1940) & (Person.birthday < d1960)))
for person in query:        
print person.name, person.birthday
#按照expression查询person名开头为小写或大写 G 的person
expression = (fn.Lower(fn.Substr(Person.name, 1, 1)) == 'g')
for person in Person.select().where(expression):    
print person.namePerson, Pet—>Update
操做q = User.update(active=False).where(User.registration_expired == True)q.execute()Person, Pet—>Insert
操做q = User.insert(username='admin', active=True, registration_expired=False)q.execute()Person, Pet—>Delete
操做q = User.delete().where(User.active == False)q.execute()
关闭数据库db.close()
总结

关于peewee还有不少具体的东西,我这里只罗列了一些基本的操做,开发过程当中须要了解更多的内容,请参照peewee官方API,见:peeweeAPI

相关文章
相关标签/搜索