成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

Python LMDB庫的使用示例

瀏覽:101日期:2022-06-27 18:35:21

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ù),則輸出Noneprint 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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章: