Peewee系列:
Peewee 使用
Peewee使用之事務(wù)
Peewee批量插入數(shù)據(jù)
Peewee 使用(二)——增刪改查更詳細(xì)使用
Peewee是一個簡單小巧的Python ORM熙涤,它非常容易學(xué)習(xí)作媚,并且使用起來很直觀。
如果想快速入門叁征,請參考官方的Quckstart尸红。
本文吱涉,只是寫今天在使用過程中的一些記錄。
基本知識
在官方的Quckstart中外里,我了解到怎爵,Peewee中Model
類、fields
和model實(shí)例
與數(shù)據(jù)庫的映射關(guān)系如下:
也就是說盅蝗,一個Model類代表一個數(shù)據(jù)庫的表
鳖链,一個Field字段代表數(shù)據(jù)庫中的一個字段
,而一個model類實(shí)例化對象則代表數(shù)據(jù)庫中的一行
墩莫。
至于Peewee的實(shí)現(xiàn)原理芙委,我暫時沒有看源代碼,但覺得和廖雪峰老師的使用元類這個文章的例子實(shí)現(xiàn)類似狂秦。
實(shí)踐
而使用過程题山,分成兩步:
定義Model,建立數(shù)據(jù)庫
在使用的時候故痊,根據(jù)需求先定義好Model顶瞳,然后可以通過create_tables()
創(chuàng)建表,若是已經(jīng)創(chuàng)建好數(shù)據(jù)庫表了,可以通過python -m pwiz
腳本工具直接創(chuàng)建Model慨菱。
第一種方式:
先定義Model焰络,然后通過db.create_tables()
創(chuàng)建或Model.create_table()
創(chuàng)建表。
例如符喝,我們需要建一個Person表闪彼,里面有name
、birthday
和is_relative
三個字段协饲,我們定義的Model如下:
from peewee import *
# 連接數(shù)據(jù)庫
database = MySQLDatabase('test', user='root', host='localhost', port=3306)
# 定義Person
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = database
然后畏腕,我們就可以創(chuàng)建表了
# 創(chuàng)建表
Person.create_table()
# 創(chuàng)建表也可以這樣, 可以創(chuàng)建多個
# database.create_tables([Person])
其中,CharField茉稠、DateField描馅、BooleanField等這些類型與數(shù)據(jù)庫中的數(shù)據(jù)類型一一對應(yīng),我們直接使用它就行而线,至于CharField => varchar(255)
這種轉(zhuǎn)換Peewee已經(jīng)為我們做好了 铭污。
第二種方式:
已經(jīng)存在過數(shù)據(jù)庫,則直接通過python -m pwiz
批量創(chuàng)建Model膀篮。
例如嘹狞,上面我已經(jīng)創(chuàng)建好了test
庫,并且創(chuàng)建了Person
表誓竿,表中擁有id
磅网、name
、birthday
和is_relative
字段筷屡。那么知市,我可以使用下面命令:
# 指定mysql,用戶為root速蕊,host為localhost嫂丙,數(shù)據(jù)庫為test
python -m pwiz -e mysql -u root -H localhost --password test > testModel.py
然后,輸入密碼规哲,pwiz
腳本會自動創(chuàng)建Model跟啤,內(nèi)容如下:
from peewee import *
database = MySQLDatabase('test', **{'charset': 'utf8', 'use_unicode': True, 'host': 'localhost', 'user': 'root', 'password': ''})
class UnknownField(object):
def __init__(self, *_, **__): pass
class BaseModel(Model):
class Meta:
database = database
class Person(BaseModel):
birthday = DateField()
is_relative = IntegerField()
name = CharField()
class Meta:
table_name = 'person'
操作數(shù)據(jù)庫
操作數(shù)據(jù)庫,就是增唉锌、刪隅肥、改和查。
一袄简、增
直接創(chuàng)建示例腥放,然后使用save()就添加了一條新數(shù)據(jù)
# 添加一條數(shù)據(jù)
p = Person(name='liuchungui', birthday=date(1990, 12, 20), is_relative=True)
p.save()
二、刪
使用delete().where().execute()進(jìn)行刪除绿语,where()是條件秃症,execute()負(fù)責(zé)執(zhí)行語句候址。若是已經(jīng)查詢出來的實(shí)例,則直接使用delete_instance()刪除种柑。
# 刪除姓名為perter的數(shù)據(jù)
Person.delete().where(Person.name == 'perter').execute()
# 已經(jīng)實(shí)例化的數(shù)據(jù), 使用delete_instance
p = Person(name='liuchungui', birthday=date(1990, 12, 20), is_relative=False)
p.id = 1
p.save()
p.delete_instance()
三岗仑、改
若是,已經(jīng)添加過數(shù)據(jù)的的實(shí)例或查詢到的數(shù)據(jù)實(shí)例聚请,且表擁有primary key
時荠雕,此時使用save()就是修改數(shù)據(jù);若是未擁有實(shí)例驶赏,則使用update().where()進(jìn)行更新數(shù)據(jù)炸卑。
# 已經(jīng)實(shí)例化的數(shù)據(jù),指定了id這個primary key,則此時保存就是更新數(shù)據(jù)
p = Person(name='liuchungui', birthday=date(1990, 12, 20), is_relative=False)
p.id = 1
p.save()
# 更新birthday數(shù)據(jù)
q = Person.update({Person.birthday: date(1983, 12, 21)}).where(Person.name == 'liuchungui')
q.execute()
四、查
單條數(shù)據(jù)使用Person.get()就行了煤傍,也可以使用Person.select().where().get()盖文。若是查詢多條數(shù)據(jù),則使用Person.select().where()患久,去掉get()就行了。語法很直觀浑槽,select()就是查詢蒋失,where是條件,get是獲取第一條數(shù)據(jù)桐玻。
# 查詢單條數(shù)據(jù)
p = Person.get(Person.name == 'liuchungui')
print(p.name, p.birthday, p.is_relative)
# 使用where().get()查詢
p = Person.select().where(Person.name == 'liuchungui').get()
print(p.name, p.birthday, p.is_relative)
# 查詢多條數(shù)據(jù)
persons = Person.select().where(Person.is_relative == True)
for p in persons:
print(p.name, p.birthday, p.is_relative)