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

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

Python的輕量級ORM框架peewee使用教程

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

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

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

它的安裝非常簡單:

pip install peewee

如果你在使用mysql數(shù)據(jù)庫的過程中報出如下錯誤:

peewee.ImproperlyConfigured: MySQL driver not installed!

則需要安裝一個mysql的驅(qū)動:

pip install pymysql

peewee的whl包是880kB,pymysql的whl包是51KB,非常輕量級。

peewee的官方文檔地址:http://docs.peewee-orm.com/en/latest/index.html

下面測試一下各項功能:

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ù)據(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ù)據(jù),指定了id這個primary key,則此時保存就是更新數(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)

下面測試一個各個方法。

測試創(chuàng)建表:

if __name__=='__main__': Person.create_table()

執(zhí)行完畢,檢查數(shù)據(jù)庫成功創(chuàng)建下面這張表:

測試插入數(shù)據(jù):

if __name__=='__main__': p = Person(name=’小華’, birthday=date(1996, 12, 20), is_relative=True) p.save()

執(zhí)行完畢后,表數(shù)據(jù)多了一行:

測試查詢數(shù)據(jù):

if __name__=='__main__': p = Person.get(Person.name == ’小華’) print(p.name, p.birthday, p.is_relative)

結(jié)果:

小華 1996-12-20 True測試刪除數(shù)據(jù):

if __name__=='__main__': Person.delete().where(Person.name == ’小華’).execute()

執(zhí)行后,數(shù)據(jù)庫對應(yīng)的記錄被刪除:

測試修改數(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()測試批量查詢:

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的輕量級ORM框架peewee使用教程的詳細內(nèi)容,更多關(guān)于Python的輕量級ORM框架peewee的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 免费视频久久 | 国产一级毛片免 | 真实国产普通话对白乱子子伦视频 | 日本在线亚州精品视频在线 | 国产精品亲子乱子伦xxxx裸 | 日本三级一区 | α级毛片 | 婷婷尹人香蕉久久天堂 | 免费看片亚洲 | 亚洲在线天堂 | 美女又黄又免费的视频 | 亚洲精品一区二区三区四区 | 免看一级a毛片一片成人不卡 | 中文在线最新版天堂 | 一级做a爰全过程免费视频毛片 | 国内精品不卡一区二区三区 | 国产精品久久久久久免费 | 久久国产夜色精品噜噜亚洲a | 国产菲菲视频在线观看 | 国产原创91 | 嫩小性性性xxxxbbbb | 亚洲欧美日韩另类精品一区二区三区 | 成人a毛片久久免费播放 | 乱系列中文字幕在线视频 | 精品久久在线 | 久久爽久久爽久久免费观看 | 久久久国产99久久国产久 | 国产精品6 | 亚洲网址在线观看 | 久久精品九九 | 亚洲日本在线观看 | 国产精品黄 | 欧美性色黄在线视 | 操欧美美女 | 99久久国产综合精品网成人影院 | 国产精品视频免费一区二区三区 | 国产午夜亚洲精品国产 | 在线播放成人高清免费视频 | 亚洲综合色一区二区三区小说 | 美女福利视频国产片 | 草草久久97超级碰碰碰免费 |