Python uses the BerkeleyDB database instance

Source: Internet
Author: User
This article mainly introduces how to use the BerkeleyDB database in python, describes the complete operation process in the form of instances, and summarizes the specific operation steps, which is very practical, for more information about how to use the Berkeley DB database in python, see the following example.

The specific implementation method is as follows:

Try: from bsddb import db failed t ImportError: from bsddb3 import db print db. DB_VERSION_STRING # check whether bsddb package def irecords (curs): record = curs. first () while record: yield record = curs. next () adb = db. DB () adb. open ('Db _ filename', dbtype = db. DB_HASH, flags = db. DB_CREATE) for I, w in enumerate ('Some word for example '. split (): adb. put (w, str (I) for key, data in irecords (adb. cursor (): print key, data adb. close () print '* 60 # the_same_db = db. DB () the_same_db.open ("db_filename") the_same_db.put ('skidoo', '23') # join the database the_same_db.put ('for', 'Change the data ') # Change the database data for key, data in irecords (the_same_db.cursor (): print key, data the_same_db.close ()

The running result is as follows:

Berkeley DB 4.7.25: (May 15, 2008)example 3some 0word 1for 2************************************************************example 3some 0word 1for change the dataskidoo 23

Here we will summarize the Operation steps:

1. initialize the database first

adb = db.DB()

2. open the database

adb.open('db_filename',dbtype = db.DB_HASH, flags = db.DB_CREATE)

3. Insert or modify data in the database

Adb. put ('skidoo', '23') # Add database adb. put ('For ', 'Change the data') # change database data

4. shut down the database

adb.close()

I hope this article will help you with Python programming.

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.