Python的輕量級(jí)ORM框架peewee使用教程
ORM框架使用最廣泛的就是SQLAlchemy和Django自帶的ORM框架,但是SQLAlchemy的語(yǔ)法顯然相對(duì)Django的ORM框架麻煩一點(diǎn)。
而Django本身是一個(gè)web框架,比較重量級(jí),僅僅為了使用Django的ORM框架的功能,而安裝Django有點(diǎn)導(dǎo)致系統(tǒng)臃腫。而peewee這個(gè)框架語(yǔ)法幾乎與Django的ORM框架一致,而又非常輕量。
它的安裝非常簡(jiǎn)單:
pip install peewee
如果你在使用mysql數(shù)據(jù)庫(kù)的過(guò)程中報(bào)出如下錯(cuò)誤:
peewee.ImproperlyConfigured: MySQL driver not installed!
則需要安裝一個(gè)mysql的驅(qū)動(dòng):
pip install pymysql
peewee的whl包是880kB,pymysql的whl包是51KB,非常輕量級(jí)。
peewee的官方文檔地址:http://docs.peewee-orm.com/en/latest/index.html
下面測(cè)試一下各項(xiàng)功能:
from peewee import *
db = MySQLDatabase('test', host="localhost", user='root', passwd='123456', port=3306)
# 定義Person
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = db
def test_create():
Person.create_table()
# 創(chuàng)建多張表也可以這樣
# database.create_tables([Person])
def test_insert():
# 添加一條數(shù)據(jù)
p = Person(name='小華', birthday=date(1996, 12, 20), is_relative=True)
p.save()
def test_delete():
# 刪除姓名為perter的數(shù)據(jù)
Person.delete().where(Person.name == 'perter').execute()
# 已經(jīng)實(shí)例化的數(shù)據(jù), 使用delete_instance
p = Person(name='小華', birthday=date(1996, 12, 20), is_relative=False)
p.id = 1
p.save()
p.delete_instance()
def test_update():
# 已經(jīng)實(shí)例化的數(shù)據(jù),指定了id這個(gè)primary key,則此時(shí)保存就是更新數(shù)據(jù)
p = Person(name='小華', birthday=date(1996, 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 == '小華')
q.execute()
def test_query():
# 查詢單條數(shù)據(jù)
p = Person.get(Person.name == '小華')
print(p.name, p.birthday, p.is_relative)
# 使用where().get()查詢
p = Person.select().where(Person.name == '小華').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)
下面測(cè)試一個(gè)各個(gè)方法。
測(cè)試創(chuàng)建表:
if __name__=="__main__": Person.create_table()
執(zhí)行完畢,檢查數(shù)據(jù)庫(kù)成功創(chuàng)建下面這張表:
測(cè)試插入數(shù)據(jù):
if __name__=="__main__": p = Person(name='小華', birthday=date(1996, 12, 20), is_relative=True) p.save()
執(zhí)行完畢后,表數(shù)據(jù)多了一行:
測(cè)試查詢數(shù)據(jù):
if __name__=="__main__": p = Person.get(Person.name == '小華') print(p.name, p.birthday, p.is_relative)
結(jié)果:
小華 1996-12-20 True
測(cè)試刪除數(shù)據(jù):
if __name__=="__main__": Person.delete().where(Person.name == '小華').execute()
執(zhí)行后,數(shù)據(jù)庫(kù)對(duì)應(yīng)的記錄被刪除:
測(cè)試修改數(shù)據(jù):
if __name__ == "__main__":
p = Person(name='小新', birthday=date(1995, 6, 20), is_relative=False)
p.save()
# 更新birthday數(shù)據(jù)
q = Person.update({Person.birthday: date(1983, 5, 21)}).where(Person.name == '小新')
q.execute()
測(cè)試批量查詢:
if __name__ == "__main__":
for i in range(1, 5):
p = Person(name=f'小張{i}', birthday=date(1995, 6, 20), is_relative=False)
p.save()
# 查詢多條數(shù)據(jù)
persons = Person.select().where(Person.is_relative == False)
for p in persons:
print(p.name, p.birthday, p.is_relative)
以上就是Python的輕量級(jí)ORM框架peewee使用教程的詳細(xì)內(nèi)容,更多關(guān)于Python的輕量級(jí)ORM框架peewee的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
OpenCV凸包檢測(cè)和凸缺陷學(xué)習(xí)示例
這篇文章主要為大家介紹了OpenCV凸包檢測(cè)和凸缺陷學(xué)習(xí)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
解決python中畫(huà)圖時(shí)x,y軸名稱出現(xiàn)中文亂碼的問(wèn)題
今天小編就為大家分享一篇解決python中畫(huà)圖時(shí)x,y軸名稱出現(xiàn)中文亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
OpenCV特征匹配和單應(yīng)性矩陣查找對(duì)象詳解
這篇文章主要為大家介紹了OpenCV特征匹配和單應(yīng)性矩陣查找對(duì)象詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
python實(shí)現(xiàn)的登錄和操作開(kāi)心網(wǎng)腳本分享
這篇文章主要介紹了python實(shí)現(xiàn)的登錄和操作開(kāi)心網(wǎng)腳本分享,可以登錄開(kāi)心網(wǎng),登錄后發(fā)送信息等功能,需要的朋友可以參考下2014-07-07
python教程對(duì)函數(shù)中的參數(shù)進(jìn)行排序
這篇文章主要介紹了python教程對(duì)函數(shù)中的參數(shù)進(jìn)行排序的方法講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-09-09
Python中的錯(cuò)誤和異常處理簡(jiǎn)單操作示例【try-except用法】
這篇文章主要介紹了Python中的錯(cuò)誤和異常處理簡(jiǎn)單操作,結(jié)合實(shí)例形式分析了Python中try except在錯(cuò)誤與異常處理中的用法,需要的朋友可以參考下2017-07-07
Python實(shí)現(xiàn)的旋轉(zhuǎn)數(shù)組功能算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)的旋轉(zhuǎn)數(shù)組功能算法,結(jié)合實(shí)例形式總結(jié)分析了數(shù)組旋轉(zhuǎn)算法的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-02-02

