Python module-bsddb: bdb High-Performance Embedded Database 1. Basic Knowledge

Source: Internet
Author: User
The bsddb module is used to operate bdb. bdb is a famous Berkeley dB with excellent performance. MySQL's storage backend engine supports bdb. Here is a brief introduction to the use of bsddb.

Different from relational databases, bdb stores only one pair of data consisting of key and value. It is used just like a python dictionary and cannot directly represent multiple fields, when you want to store data with multiple fields, you can only store the data into the value as a whole.
What data access method does bsddb face? bdb supports four types: btree, hash, queue, and recno. Let's talk about the differences between them. B-tree stores data only by using the tree structure. The query speed is very fast and any complicated key and value can be stored. Hash is a hash algorithm. Its speed is similar to that of B-tree. However, hash should be used when the data volume is extremely large. Queue is a queue operation. It has a limitation that it can only store a fixed length of data, that is, the length of value is fixed! However, the queue can keep the data first-in-first-out, optimize data insertion, and provide row-level locks. The key of the queue must be a number. Recno is similar to queue, But it supports variable-length values. Its key is also a number.

Here, we will first demonstrate how to open the database and insert a data entry for these four data access methods.
For the python bsddb module, there are two ways to open a database: one is to use the original interface, that is, to open an environment and then open a database from this environment, like the following:

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)

Another method exclusive to python is that the bsddb module encapsulates the above process, such as opening the btree:

Import bsddb

DB = bsddb. btopen ('test. db', 'C ')

It looks much simpler than above. However, this method provides limited interfaces and only simple functions. It is not the first method, but it is thread-safe in python2.5. We will introduce it here.
Let's look at an example:
#-*-Encoding: gb2312 -*-
Import OS, sys, string
Import bsddb, time

Home = "db_home"
Filename = "test. DB"
Try:
# Create the Home Directory
OS. mkdir (home)
Except t:
Pass

# Creating a database environment
Dbenv = bsddb. DB. dbenv ()
# Open the database environment
Dbenv. Open (home, bsddb. DB. db_create | bsddb. DB. db_init_mpool)
# Creating database objects
D = bsddb. DB. dB (dbenv)
# Open the database. The second parameter specifies the data access method used.
# Btree is bsddb. DB. db_btree, and hash is bsddb. DB. db_hash
# Queu is bsddb. DB. db_queue, and recno is bsddb. DB. db_recno
D. Open (filename, bsddb. DB. db_btree, bsddb. DB. db_create, 0666)
# Insert a data record. Note that the keys of queue and recno cannot be strings or numbers.
D. Put ('test1', 'zhaowei ')
Print D. Items ()
# Close. Data is written back to the file.
D. Close ()
Dbenv. Close ()

The following describes how to use queue:

#-*-Encoding: gb2312 -*-
Import OS, sys, string
Import bsddb, time

Home = "db_home"
Filename = "testqueue. DB"
Try:
OS. mkdir (home)
Except t:
Pass

Dbenv = bsddb. DB. dbenv ()
Dbenv. Open (home, bsddb. DB. db_create | bsddb. DB. db_init_mpool)
D = bsddb. DB. dB (dbenv)
# The length of a value must be set for the queue. Its value is fixed length.
D. set_re_len (40)
D. Open (filename, bsddb. DB. db_queue, bsddb. DB. db_create, 0666)
# Its key must be a number
D. Put (1, 'zhaowei ')
Print D. Items ()

D. Close ()
Dbenv. Close ()

The second simple method is as follows, which is much simpler:

Import bsddb

D = bsddb. hashopen ("AAA. DB", "C ")
D ['test1'] = "zhaowei"
Print D. Items ()
D. Close ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.