国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python的輕量級(jí)ORM框架peewee使用教程

瀏覽:69日期:2022-06-28 11:16:19

ORM框架使用最廣泛的就是SQLAlchemy和Django自帶的ORM框架,但是SQLAlchemy的語法顯然相對(duì)Django的ORM框架麻煩一點(diǎn)。

而Django本身是一個(gè)web框架,比較重量級(jí),僅僅為了使用Django的ORM框架的功能,而安裝Django有點(diǎn)導(dǎo)致系統(tǒng)臃腫。而peewee這個(gè)框架語法幾乎與Django的ORM框架一致,而又非常輕量。

它的安裝非常簡(jiǎn)單:

pip install peewee

如果你在使用mysql數(shù)據(jù)庫的過程中報(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)# 定義Personclass 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ù)庫成功創(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ù)庫對(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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久久久久综合七次郎 | 国产精品久久久久毛片 | 免费看欧美xxx片 | 日韩高清一区二区 | 在线免费一级片 | 成人午夜兔费观看网站 | 久久精品国产99国产精品免费看 | 波多野结衣一区在线 | 中国一级毛片在线观看 | 美女被免费网站在线视频软件 | 模特视频一二三区 | 日本亚洲成高清一区二区三区 | 日本一级爽毛片在线看 | 成年女人毛片 | 中文字幕人成不卡一区 | 日韩高清不卡在线 | 长腿美女被啪的欲仙欲死视频 | 国产一区二区三区在线观看精品 | 69日本xxxxxxxxx13 69视频成人 | 亚欧美图片自偷自拍另类 | 国产香港特级一级毛片 | 欧美高清一级毛片免费视 | 在线免费观看国产视频 | 农村寡妇一级毛片免费看视频 | 亚洲第一看片 | 久久久国产99久久国产久 | 日韩精品一区二区三区不卡 | 精品国产高清在线看国产 | 中国女人真人一级毛片 | 亚洲一区精品在线 | 国产日韩一区二区三区 | 欧美激情久久久久久久大片 | 国产成人高清亚洲一区91 | 久久这里只有精品免费播放 | 瑟瑟网站在线观看 | 91久色视频 | 国产免费一区不卡在线 | 欧洲女同互慰在线视频 | 91久久99热青草国产 | 日本一在线中文字幕天堂 | 国产一区二区影视 |