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ì)指南。
請選擇以下任一種方式輸入命令安裝依賴 :
- Windows 環(huán)境 打開 Cmd (開始-運(yùn)行-CMD)。
- MacOS 環(huán)境 打開 Terminal (command+空格輸入Terminal)。
- 如果你用的是 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'))
-
數(shù)據(jù)庫
+關(guān)注
關(guān)注
7文章
3900瀏覽量
65767 -
應(yīng)用程序
+關(guān)注
關(guān)注
38文章
3322瀏覽量
58719 -
python
+關(guān)注
關(guān)注
56文章
4825瀏覽量
86166
發(fā)布評論請先 登錄
【建議收藏】Python庫大全
木棉花:輕量級偏好數(shù)據(jù)庫學(xué)習(xí)筆記--沈泳鑫
基于輕量級偏好數(shù)據(jù)庫,實(shí)現(xiàn)存儲在本地應(yīng)用數(shù)據(jù)的訪問及操作
HarmonyOS數(shù)據(jù)庫的相關(guān)資料下載
深度剖析OpenHarmony輕量級數(shù)據(jù)存儲
Android游戲開發(fā)之SQLite數(shù)據(jù)庫

評論