Python LMDB庫的使用示例
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)文章!
相關(guān)文章:
1. asp在iis7報錯行號不準(zhǔn)問題的解決方法2. 三個不常見的 HTML5 實用新特性簡介3. ASP中解決“對象關(guān)閉時,不允許操作。”的詭異問題……4. 原生js XMLhttprequest請求onreadystatechange執(zhí)行兩次的解決5. 刪除docker里建立容器的操作方法6. CSS代碼檢查工具stylelint的使用方法詳解7. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法8. jsp實現(xiàn)簡單用戶7天內(nèi)免登錄9. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效10. 匹配模式 - XSL教程 - 4
