Python BSDDB module Operation Berkeley DB Database Introduction _python

Source: Internet
Author: User
Tags mkdir create database

BSDDB module is used to operate the BDB module, BDB is the famous Berkeley DB, its performance is very good, MySQL's storage back-end engine support BDB way. Here is a brief introduction to some of the ways in which BSDDB is used.

Unlike a general relational database, BDB stores only a pair of data that consists of key and value, which, like a Python dictionary, cannot directly represent multiple fields, and when data is stored in multiple fields, data can only be stored in value as a whole.

The first issue facing the use of BSDDB is what data access method is used, BDB supports four kinds: btree, hash, queue, recno. Here to say what is the difference between them, btree is used in the tree structure to store data, query speed quickly, can store arbitrary complex key and value. Hash is used in the hash algorithm, the speed is actually similar to Btree, but when the volume of data is particularly large, you should use hash. Queues are queue operations, which have a limit that can only store fixed-length data, meaning that value is fixed! However, the queue can keep the data advanced first out, and the insertion of the data to make a special optimization, and provide row-level locks. The key of the queue must be a number. Recno is similar to queue, but it can support a variable length value, and its key is also a number.

Here first of these four methods of data access to open the database, the simple insertion of a demonstration of data.
For the Python bsddb module, there are two ways to open a database, one is to use the original interface, which is to open an environment and then open a database from the environment, as follows:

Copy Code code as follows:

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 way is Python-specific, this is the BSDDB module itself to the process of the above packaging, such as open btree:

Copy Code code as follows:

Import BSDDB

db = Bsddb.btopen (' test.db ', ' C ')

It looks a lot simpler than the above. However, the interface provided in this way is very limited and has only a very simple function, without the first flexibility, but it is thread-safe in the python2.5 version. Here are the introductions.
Take a look at an example:

Copy Code code as follows:

#-*-encoding:gb2312-*-
Import OS, sys, string
Import Bsddb, Time

Home = "Db_home"
filename = "Test.db"
Try
# Create home Directory
Os.mkdir (Home)
Except
Pass

# Create a database environment
Dbenv = BSDDB.DB.DBENV ()
# Open Database Environment
Dbenv.open (home, Bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL)
# CREATE Database objects
D = Bsddb.db.DB (dbenv)
# Open the database, the second parameter here is to specify what data access method to use
# Btree is Bsddb.db.DB_BTREE, HASH is Bsddb.db.DB_HASH
# Queu is Bsddb.db.DB_QUEUE, Recno is Bsddb.db.DB_RECNO
D.open (filename, Bsddb.db.DB_BTREE, Bsddb.db.DB_CREATE, 0666)
# Insert a piece of data, note that the queue and RECNO key can not be a string, should be a number
D.put (' test1 ', ' Zhaowei ')
Print D.items ()
# Close, when the data is written back to the file
D.close ()
Dbenv.close ()


Here is a use of queue, pay attention to see what the difference:
Copy Code code as follows:

#-*-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 must be set to a value of length, 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 simple second way to use the following is much simpler:
Copy Code code as follows:

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.