Python LMDB庫的使用示例
更新時間:2021年02月14日 14:47:51 作者:只言片語
這篇文章主要介紹了Python LMDB庫的使用示例,幫助大家更好的利用python處理數(shù)據(jù)庫,感興趣的朋友可以了解下
linux中,可以使用指令
pip install lmdb
安裝lmdb包。
----
- lmdb 數(shù)據(jù)庫文件生成
- 增 改 刪
- 查
1、生成一個空的lmdb數(shù)據(jù)庫文件
# -*- coding: utf-8 -*-
import lmdb
# 如果train文件夾下沒有data.mbd或lock.mdb文件,則會生成一個空的,如果有,不會覆蓋
# map_size定義最大儲存容量,單位是kb,以下定義1TB容量
env = lmdb.open("./train",map_size=1099511627776)
env.close()
2、LMDB數(shù)據(jù)的添加、修改、刪除
# -*- coding: utf-8 -*-
import lmdb
# map_size定義最大儲存容量,單位是kb,以下定義1TB容量
env = lmdb.open("./train", map_size=1099511627776)
txn = env.begin(write=True)
# 添加數(shù)據(jù)和鍵值
txn.put(key = '1', value = 'aaa')
txn.put(key = '2', value = 'bbb')
txn.put(key = '3', value = 'ccc')
# 通過鍵值刪除數(shù)據(jù)
txn.delete(key = '1')
# 修改數(shù)據(jù)
txn.put(key = '3', value = 'ddd')
# 通過commit()函數(shù)提交更改
txn.commit()
env.close()
3、查詢LMDB數(shù)據(jù)庫
# -*- coding: utf-8 -*-
import lmdb
env = lmdb.open("./train")
# 參數(shù)write設(shè)置為True才可以寫入
txn = env.begin(write=True)
############################################添加、修改、刪除數(shù)據(jù)
# 添加數(shù)據(jù)和鍵值
txn.put(key = '1', value = 'aaa')
txn.put(key = '2', value = 'bbb')
txn.put(key = '3', value = 'ccc')
# 通過鍵值刪除數(shù)據(jù)
txn.delete(key = '1')
# 修改數(shù)據(jù)
txn.put(key = '3', value = 'ddd')
# 通過commit()函數(shù)提交更改
txn.commit()
############################################查詢lmdb數(shù)據(jù)
txn = env.begin()
# get函數(shù)通過鍵值查詢數(shù)據(jù)
print txn.get(str(2))
# 通過cursor()遍歷所有數(shù)據(jù)和鍵值
for key, value in txn.cursor():
print (key, value)
############################################
env.close()
4. 讀取已有.mdb文件內(nèi)容
# -*- coding: utf-8 -*-
import lmdb
env_db = lmdb.Environment('trainC')
# env_db = lmdb.open("./trainC")
txn = env_db.begin()
# get函數(shù)通過鍵值查詢數(shù)據(jù),如果要查詢的鍵值沒有對應(yīng)數(shù)據(jù),則輸出None
print txn.get(str(200))
for key, value in txn.cursor(): #遍歷
print (key, value)
env_db.close()
以上就是Python LMDB庫的使用示例的詳細(xì)內(nèi)容,更多關(guān)于Python LMDB庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Numpy之random函數(shù)使用學(xué)習(xí)
這篇文章主要介紹了Numpy之random使用學(xué)習(xí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
用Python的SimPy庫簡化復(fù)雜的編程模型的介紹
這篇文章主要介紹了用Python的SimPy庫簡化復(fù)雜的編程模型的介紹,本文來自于官方的開發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04
Python使用matplotlib繪制Logistic曲線操作示例
這篇文章主要介紹了Python使用matplotlib繪制Logistic曲線操作,結(jié)合實例形式詳細(xì)分析了Python基于matplotlib庫繪制Logistic曲線相關(guān)步驟與實現(xiàn)技巧,需要的朋友可以參考下2019-11-11

