<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>

          TinyDB 一個(gè)純Python編寫的輕量級數(shù)據(jù)庫

          共 3592字,需瀏覽 8分鐘

           ·

          2022-08-29 10:35

          TinyDB 是一個(gè)純 Python 編寫的輕量級數(shù)據(jù)庫,一共只有1800行代碼,沒有外部依賴項(xiàng)。

          TinyDB的目標(biāo)是降低小型 Python 應(yīng)用程序使用數(shù)據(jù)庫的難度,對于一些簡單程序而言與其用 SQL 數(shù)據(jù)庫,不如就用TinyDB, 因?yàn)樗腥缦绿攸c(diǎn):

          • 輕便:當(dāng)前源代碼有 1800 行代碼(大約 40% 的文檔)和 1600 行測試代碼。

          • 可隨意遷移:在當(dāng)前文件夾下生成數(shù)據(jù)庫文件,不需要任何服務(wù),可以隨意遷移。

          • 簡單:TinyDB 通過提供簡單干凈的 API 使得用戶易于使用。

          • 用純 Python 編寫: TinyDB 既不需要外部服務(wù)器,也不需要任何來自 PyPI 的依賴項(xiàng)。

          • 適用于 Python 3.6+ 和 PyPy3: TinyDB 適用于所有現(xiàn)代版本的 Python 和 PyPy。

          • 強(qiáng)大的可擴(kuò)展性:您可以通過編寫中間件修改存儲的行為來輕松擴(kuò)展 TinyDB。

          • 100% 測試覆蓋率:無需解釋。

          1.準(zhǔn)備


          開始之前,你要確保Python和pip已經(jīng)成功安裝在電腦上,如果沒有,可以訪問這篇文章:超詳細(xì)Python安裝指南 進(jìn)行安裝。

          (可選1) 如果你用Python的目的是數(shù)據(jù)分析,可以直接安裝Anaconda:Python數(shù)據(jù)分析與挖掘好幫手—Anaconda,它內(nèi)置了Python和pip.

          (可選2) 此外,推薦大家用VSCode編輯器,它有許多的優(yōu)點(diǎn):Python 編程的最好搭檔—VSCode 詳細(xì)指南。

          請選擇以下任一種方式輸入命令安裝依賴
          1. Windows 環(huán)境 打開 Cmd (開始-運(yùn)行-CMD)。
          2. MacOS 環(huán)境 打開 Terminal (command+空格輸入Terminal)。
          3. 如果你用的是 VSCode編輯器 或 Pycharm,可以直接使用界面下方的Terminal.

          pip install tinydb

          2.簡單的增刪改查示例


          初始化一個(gè)DB文件:

          from tinydb import TinyDB
          db = TinyDB('db.json')

          這樣就在當(dāng)前文件夾下生成了一個(gè)名為 `db.json` 的數(shù)據(jù)庫文件。

          往里面插入數(shù)據(jù)

          from tinydb import TinyDB
          db = TinyDB('db.json')
          db.insert({'type': 'apple', 'count': 7})
          db.insert({'type': 'peach', 'count': 3})

          可以看到,我們可以直接往數(shù)據(jù)庫里插入字典數(shù)據(jù),不需要任何處理。下面是批量插入的方法:

          db.insert_multiple([
              {'name': 'John', 'age': 22},
              {'name': 'John', 'age': 37}])
          db.insert_multiple({'int': 1, 'value': i} for i in range(2))

          查詢所有數(shù)據(jù)

          from tinydb import TinyDB
          db = TinyDB('db.json')
          db.all()
          # [{'count': 7, 'type': 'apple'}, {'count': 3, 'type': 'peach'}]

          除了 .all() 我們還可以使用for循環(huán)遍歷db:

          from tinydb import TinyDB
          db = TinyDB('db.json')
          for item in db:
              print(item)
          # {'count': 7, 'type': 'apple'}
          # {'count': 3, 'type': 'peach'}

          如果你需要搜索特定數(shù)據(jù),可以使用Query():

          from tinydb import TinyDB
          db = TinyDB('db.json')
          Fruit = Query()
          db.search(Fruit.type == 'peach')
          # [{'count': 3, 'type': 'peach'}]
          db.search(Fruit.count > 5)
          # [{'count': 7, 'type': 'apple'}]

          更新數(shù)據(jù):

          from tinydb import TinyDB
          db = TinyDB('db.json')
          db.update({'foo': 'bar'})

          # 刪除某個(gè)Key
          from tinydb.operations import delete
          db.update(delete('key1'), User.name == 'John')

          刪除數(shù)據(jù)

          刪除數(shù)據(jù)也可以使用類似的條件語句:

          from tinydb import TinyDB
          db = TinyDB('db.json')
          db.remove(Fruit.count < 5)
          db.all()
          # [{'count': 10, 'type': 'apple'}]

          清空整個(gè)數(shù)據(jù)庫:

          from tinydb import TinyDB
          db = TinyDB('db.json')
          db.truncate()
          db.all()
          # []

          3.高級查詢


          除了點(diǎn)操作符訪問數(shù)據(jù),你還可以用原生的dict訪問表示法:

          # 寫法1
          db.search(User.country-code == 'foo')
          # 寫法2
          db.search(User['country-code'] == 'foo')

          這兩種寫法是等效的。

          另外在常見的查詢運(yùn)算符(==, <, >, ...)之外,TinyDB還支持where語句:

          from tinydb import where
          db.search(where('field') == 'value')

          這等同于:

          db.search(Query()['field'] == 'value')

          這種語法還能訪問嵌套字段:

          db.search(where('birthday').year == 1900)
          # 或者
          db.search(where('birthday')['year'] == 1900)

          Any 查詢方法:

          db.search(Group.permissions.any(Permission.type == 'read'))
          # [{'name': 'user', 'permissions': [{'type': 'read'}]},
          # {'name': 'sudo', 'permissions': [{'type': 'read'}, {'type': 'sudo'}]},
          # {'name': 'admin', 'permissions':
          # [{'type': 'read'}, {'type': 'write'}, {'type': 'sudo'}]}]

          檢查單個(gè)項(xiàng)目是否包含在列表中:

          db.search(User.name.one_of(['jane', 'john']))

          TinyDB還支持和Pandas類似的邏輯操作:

          # Negate a query:
          db.search(~ (User.name == 'John'))
          # Logical AND:
          db.search((User.name == 'John') & (User.age <= 30))
          # Logical OR:
          db.search((User.name == 'John') | (User.name == 'Bob'))

          TinyDB的介紹就到這里,你還可以訪問他們的官方文檔,查看更多的使用方法:

          https://tinydb.readthedocs.io/en/latest/usage.html

          尤其是想基于TinyDB做些存儲優(yōu)化的同學(xué),你們可以詳細(xì)閱讀 Storage & Middleware 章節(jié)。

          我們的文章到此就結(jié)束啦,如果你喜歡今天的Python 實(shí)戰(zhàn)教程,請持續(xù)關(guān)注Python實(shí)用寶典。

          有任何問題,可以在公眾號后臺回復(fù):加群,回答相應(yīng)紅字驗(yàn)證信息,進(jìn)入互助群詢問。

          原創(chuàng)不易,希望你能在下面點(diǎn)個(gè)贊和在看支持我繼續(xù)創(chuàng)作,謝謝!

          點(diǎn)擊下方閱讀原文可獲得更好的閱讀體驗(yàn)

          Python實(shí)用寶典 (pythondict.com)
          不只是一個(gè)寶典
          歡迎關(guān)注公眾號:Python實(shí)用寶典

          瀏覽 55
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  黄色电影A片 | 欧美黄色大片免费在线观看 | 色色干干 | 日韩黄色免费爱爱 | 人妻在线中文字幕 |