bsddb模块是用来操作bdb的模块,bdb是著名的Berkeley DB,它的性能非常好,mysql的存储后端引擎都支持bdb的方式。这里简单介绍一些关于bsddb的使用方法。
bdb不同于一般的关系数据库,它存储的数据只能是以key和value组成的一对数据,使用就像python的字典一样,它不能直接表示多个字段,当要存储多个字段的数据时,只能把数据作为一个整体存放到value中。
使用bsddb面临的第一问题是使用什么数据访问方法,bdb支持四种:btree, hash, queue, recno。这里先说说它们有什么区别,btree是用的树结构来才存储的数据,查询速度很快,可以存储任意复杂的key和value。hash是用的hash算法,速度其实和btree比差不多的,但是当数据量特别巨大时,应该使用hash。queue是队列操作,它有一个限制,它只能存储定长的数据,也就是说value的长度是固定的!但是queue可以保持数据的先进先出,并且对数据的插入做了特殊的优化,并且提供行级锁。queue的key必须是数字。recno和queue类似,但是它可以支持变长的value,它的key同样也是数字。
这里先对这四种数据访问方法分别做打开数据库,简单插入一条数据的演示。
对于python的bsddb模块来说,打开数据库的操作有两种方式,一是使用原始的接口,就是先打开一个环境,然后从这个环境中打开一个数据库,就像下面:
代码如下:
import bsddb
dbenv = bsddb.db.DBEnv()
dbenv.open(home, bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
d = bsddb.db.DB(dbenv)
d.open(filename, bsddb.db.DB_BTREE, bsddb.db.DB_CREATE, 0666)
还有一种方式是python特有的,这个是bsddb模块本身对上面的过程做了包装,比如打开btree的:
代码如下:
import bsddb
db = bsddb.btopen('test.db', 'c')
看起来比上面的简单多了吧。但这种方式提供的接口很有限,也只有很简单的功能,没有第一种的灵活,但是它在python2.5的版本里是线程安全的。这里都介绍一下。
看看一个例子:
代码如下:
#-*- encoding: gb2312 -*-
import os, sys, string
import bsddb, time
home = "db_home"
filename = "test.db"
try:
# 创建home目录
os.mkdir(home)
except:
pass
# 创建数据库环境
dbenv = bsddb.db.DBEnv()
# 打开数据库环境
dbenv.open(home, bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
# 创建数据库对象
d = bsddb.db.DB(dbenv)
# 打开数据库, 这里的第二个参数就是指定使用什么数据访问方法
# btree是 bsddb.db.DB_BTREE, hash是bsddb.db.DB_HASH
# queu 是 bsddb.db.DB_QUEUE, recno 是bsddb.db.DB_RECNO
d.open(filename, bsddb.db.DB_BTREE, bsddb.db.DB_CREATE, 0666)
# 插入一条数据,注意queue和recno的key不能是字符串的,应该是数字
d.put('test1', 'zhaowei')
print d.items()
# 关闭,这时会把数据写回文件
d.close()
dbenv.close()
下面来个使用queue的,注意看有什么区别:
代码如下:
#-*- encoding: gb2312 -*-
import os, sys, string
import bsddb, time
home = "db_home"
filename = "testqueue.db"
try:
os.mkdir(home)
except:
pass
dbenv = bsddb.db.DBEnv()
dbenv.open(home, bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
d = bsddb.db.DB(dbenv)
# queue必须要设置一个value的长度,它的value是定长的
d.set_re_len(40)
d.open(filename, bsddb.db.DB_QUEUE, bsddb.db.DB_CREATE, 0666)
# 它的key必须是数字
d.put(1, 'zhaowei')
print d.items()
d.close()
dbenv.close()
那简单的第二种方式使用如下, 要简洁很多了:
代码如下:
import bsddb
d = bsddb.hashopen("aaa.db", "c")
d['test1'] = "zhaowei"
print d.items()
d.close()

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to solve the problem of Jieba word segmentation in scenic spot comment analysis? When we are conducting scenic spot comments and analysis, we often use the jieba word segmentation tool to process the text...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function