Peewee 使用

Peewee系列:
Peewee 使用
Peewee使用之事務(wù)
Peewee批量插入數(shù)據(jù)
Peewee 使用(二)——增刪改查更詳細使用


Peewee是一個簡單小巧的Python ORM,它非常容易學習,并且使用起來很直觀。

如果想快速入門,請參考官方的Quckstart。

本文,只是寫今天在使用過程中的一些記錄。

基本知識

官方的Quckstart中,我了解到,Peewee中Model類、fieldsmodel實例與數(shù)據(jù)庫的映射關(guān)系如下:

d

也就是說,一個Model類代表一個數(shù)據(jù)庫的表,一個Field字段代表數(shù)據(jù)庫中的一個字段,而一個model類實例化對象則代表數(shù)據(jù)庫中的一行

至于Peewee的實現(xiàn)原理,我暫時沒有看源代碼,但覺得和廖雪峰老師的使用元類這個文章的例子實現(xiàn)類似。

實踐

而使用過程,分成兩步:

定義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、birthdayis_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ù)類型一一對應,我們直接使用它就行,至于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、namebirthdayis_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()進行刪除,where()是條件,execute()負責執(zhí)行語句。若是已經(jīng)查詢出來的實例,則直接使用delete_instance()刪除。

# 刪除姓名為perter的數(shù)據(jù)
Person.delete().where(Person.name == 'perter').execute()

# 已經(jīng)實例化的數(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ù)據(jù)實例,且表擁有primary key時,此時使用save()就是修改數(shù)據(jù);若是未擁有實例,則使用update().where()進行更新數(shù)據(jù)。

# 已經(jīng)實例化的數(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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容