Python中peewee模組

來源:互聯網
上載者:User

標籤:

Python中peewee模組,有需要的朋友可以參考下。


前言關於ORM架構:

簡介:

對象關係映射(英語:Object Relational Mapping,簡稱ORM,或O/RM,或O/R mapping),是一種程式技術,用於實現物件導向程式設計語言裡不同類型系統的資料之間的轉換。從效果上說,它其實是建立了一個可在程式設計語言裡使用的“虛擬對象資料庫”。

對象關係映射(Object-Relational Mapping)提供了概念性的、易於理解的模型化資料的方法。ORM方法論基於三個核心原則: 簡單:以最基本的形式建模資料。 傳達性:資料庫結構被任何人都能理解的語言文檔化。 精確性:基於資料模型建立正確標準化了的結構。 典型地,建模者通過收集來自那些熟悉應用程式但不熟練的資料建模者的人的資訊開發資訊模型。建模者必須能夠用非技術企業專家可以理解的術語在概念層次上與資料結構進行通訊。建模者也必須能以簡單的單元分析資訊,對樣本資料進行處理。ORM專門被設計為改進這種聯絡。

ORM優勢:

1.隱藏了資料訪問細節,“封閉”的通用資料庫互動,ORM的核心。他使得我們的通用資料庫互動變得簡單易行,並且完全不用考慮該死的SQL語句。快速開發,由此而來。

2.在ORM年表的史前時代,我們需要將我們的物件模型轉化為一條一條的SQL語句,通過直連或是DB helper在關聯式資料庫構造我們的資料庫體系。而現在,基本上所有的ORM架構都提供了通過物件模型構造關聯式資料庫結構的功能。

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

 

Python中peewee模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.