Python LMDB库的使用示例
作者:只言片语 时间:2021-01-31 15:11:25
linux中,可以使用指令
pip install lmdb
安装lmdb包。
----
lmdb 数据库文件生成
增 改 删
查
1、生成一个空的lmdb数据库文件
# -*- 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数据的添加、修改、删除
# -*- coding: utf-8 -*-
import lmdb
# map_size定义最大储存容量,单位是kb,以下定义1TB容量
env = lmdb.open("./train", map_size=1099511627776)
txn = env.begin(write=True)
# 添加数据和键值
txn.put(key = '1', value = 'aaa')
txn.put(key = '2', value = 'bbb')
txn.put(key = '3', value = 'ccc')
# 通过键值删除数据
txn.delete(key = '1')
# 修改数据
txn.put(key = '3', value = 'ddd')
# 通过commit()函数提交更改
txn.commit()
env.close()
3、查询LMDB数据库
# -*- coding: utf-8 -*-
import lmdb
env = lmdb.open("./train")
# 参数write设置为True才可以写入
txn = env.begin(write=True)
############################################添加、修改、删除数据
# 添加数据和键值
txn.put(key = '1', value = 'aaa')
txn.put(key = '2', value = 'bbb')
txn.put(key = '3', value = 'ccc')
# 通过键值删除数据
txn.delete(key = '1')
# 修改数据
txn.put(key = '3', value = 'ddd')
# 通过commit()函数提交更改
txn.commit()
############################################查询lmdb数据
txn = env.begin()
# get函数通过键值查询数据
print txn.get(str(2))
# 通过cursor()遍历所有数据和键值
for key, value in txn.cursor():
print (key, value)
############################################
env.close()
4. 读取已有.mdb文件内容
# -*- coding: utf-8 -*-
import lmdb
env_db = lmdb.Environment('trainC')
# env_db = lmdb.open("./trainC")
txn = env_db.begin()
# get函数通过键值查询数据,如果要查询的键值没有对应数据,则输出None
print txn.get(str(200))
for key, value in txn.cursor(): #遍历
print (key, value)
env_db.close()
来源:https://www.cnblogs.com/xiaoniu-666/p/12191262.html
标签:Python,LMDB,库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
我们用什么来衡量设计3:定量研究的风险
2009-07-19 14:14:00
python编写接口测试文档(以豆瓣搜索为例)
2023-09-21 17:39:49
![](https://img.aspxhome.com/file/2023/2/62282_0s.png)
python之Socket网络编程详解
2021-05-29 14:43:22
![](https://img.aspxhome.com/file/2023/5/66365_0s.jpg)
Microsoft VBScript 运行时错误 错误800a0005 无效的过程调用或参数
2010-03-25 21:51:00
Python装饰器的定义和使用详情
2021-01-26 23:22:15
我的css样式写法总结
2009-01-18 13:04:00
ASP 递归调用 已知节点查找根节点的函数
2011-03-08 10:48:00
服务器XMLHTTP(Server XMLHTTP in ASP)基础
2008-11-11 12:45:00
python使用Plotly绘图工具绘制散点图、线形图
2022-06-24 08:11:50
![](https://img.aspxhome.com/file/2023/7/78607_0s.png)
从 msxml6.dll 中获取 DOMDocument 对象的方法与属性
2009-02-22 18:46:00
带你深入了解SQL Server 2008的独到之处
2009-01-07 14:20:00
itchat-python搭建微信机器人(附示例)
2022-05-04 00:00:54
企业网站FLASH引导页存在的意义
2008-06-04 11:09:00
Python with语句上下文管理器两种实现方法分析
2023-03-21 21:50:18
利用python实现可视化大屏
2023-08-17 17:29:17
![](https://img.aspxhome.com/file/2023/9/65489_0s.png)
SQL 比较一个集合是否在另一个集合里存在的方法分享
2011-12-01 10:19:26
python读取yaml文件后修改写入本地实例
2021-12-20 02:53:54
Windows下对MySQL安装的故障诊断与排除
2008-12-17 16:50:00
Python日期时间Time模块实例详解
2023-11-07 07:26:34
Python中利用ItsDangerous快捷实现数据加密
2022-06-09 23:24:41
![](https://img.aspxhome.com/file/2023/3/67283_0s.jpg)