濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Python的輕量級(jí)ORM框架peewee使用教程

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

熱門(mén)標(biāo)簽:申請(qǐng)辦個(gè)400電話號(hào)碼 外呼系統(tǒng)前面有錄音播放嗎 千呼ai電話機(jī)器人免費(fèi) 柳州正規(guī)電銷(xiāo)機(jī)器人收費(fèi) 鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 騰訊地圖標(biāo)注有什么版本 高德地圖標(biāo)注字母 深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商 400電話辦理費(fèi)用收費(fèi)

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():
  # 查詢(xún)單條數(shù)據(jù)
  p = Person.get(Person.name == '小華')
  print(p.name, p.birthday, p.is_relative)

  # 使用where().get()查詢(xún)
  p = Person.select().where(Person.name == '小華').get()
  print(p.name, p.birthday, p.is_relative)

  # 查詢(xún)多條數(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è)試查詢(xún)數(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è)試批量查詢(xún):

if __name__ == "__main__":
  for i in range(1, 5):
    p = Person(name=f'小張{i}', birthday=date(1995, 6, 20), is_relative=False)
    p.save()
  # 查詢(xún)多條數(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)文章!

您可能感興趣的文章:
  • Python ORM框架Peewee用法詳解
  • Python輕量級(jí)ORM框架Peewee訪問(wèn)sqlite數(shù)據(jù)庫(kù)的方法詳解
  • python輕量級(jí)orm框架 peewee常用功能速查詳情

標(biāo)簽:哈爾濱 平頂山 合肥 郴州 烏蘭察布 大慶 烏蘭察布 海南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python的輕量級(jí)ORM框架peewee使用教程》,本文關(guān)鍵詞  Python,的,輕量級(jí),ORM,框架,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python的輕量級(jí)ORM框架peewee使用教程》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Python的輕量級(jí)ORM框架peewee使用教程的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    彩票| 高平市| 东乡县| 托里县| 电白县| 惠东县| 西藏| 昔阳县| 扎囊县| 买车| 商水县| 山丹县| 辽阳市| 瓦房店市| 乐山市| 马鞍山市| 兴和县| 大宁县| 中西区| 通州市| 铁力市| 巩义市| 通海县| 宁陵县| 高平市| 盖州市| 双城市| 乌兰浩特市| 揭阳市| 磐石市| 陈巴尔虎旗| 荥阳市| 封丘县| 临邑县| 古田县| 巴塘县| 横峰县| 永平县| 贺兰县| 威远县| 南和县|