<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python的輕量級ORM框架peewee

          共 5383字,需瀏覽 11分鐘

           ·

          2021-03-03 22:16


          專欄作者:小小明

          非常擅長解決各類復(fù)雜數(shù)據(jù)處理的邏輯,各類結(jié)構(gòu)化與非結(jié)構(gòu)化數(shù)據(jù)互轉(zhuǎn),字符串解析匹配等等。至今已經(jīng)幫助很多數(shù)據(jù)從業(yè)者解決工作中的實際問題,如果你在數(shù)據(jù)處理上遇到什么困難,歡迎評論區(qū)與我交流。

          在前面的《改變python對象規(guī)則的黑魔法metaclass》一文中,我介紹了使用metaclass自己編寫ORM框架的思路。

          當然python庫中這類框架非常多,我們并沒有必要自己去實現(xiàn)。

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

          而Django本身是一個web框架,比較重量級,僅僅為了使用Django的ORM框架的功能,而安裝Django有點導(dǎo)致系統(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)


          # 定義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(19961220), 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(19961220), 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(19961220), is_relative=False)
              p.id = 1
              p.save()

              # 更新birthday數(shù)據(jù)
              q = Person.update({Person.birthday: date(19831221)}).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(19961220), 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(1995620), is_relative=False)
              p.save()
              # 更新birthday數(shù)據(jù)
              q = Person.update({Person.birthday: date(1983521)}).where(Person.name == '小新')
              q.execute()

          執(zhí)行后:

          測試批量查詢:

          if __name__ == "__main__":
              for i in range(15):
                  p = Person(name=f'小張{i}', birthday=date(1995620), 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)

          執(zhí)行后:

          結(jié)果:

          小新 1983-05-21 False
          小張1 1995-06-20 False
          小張2 1995-06-20 False
          小張3 1995-06-20 False
          小張4 1995-06-20 False

          更多用法可參考官方文檔。

          — 【 THE END 】—
          本公眾號全部博文已整理成一個目錄,請在公眾號里回復(fù)「m」獲?。?/span>


          3T技術(shù)資源大放送!包括但不限于:Java、C/C++,Linux,Python,大數(shù)據(jù),人工智能等等。在公眾號內(nèi)回復(fù)「1024」,即可免費獲?。?!




          瀏覽 52
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  99久久免费看 | 久久人热视频综合网 | 无码视频在线观看免费 | 在线看黄片视频 | 久久9999 |