Python uses a Berkeley DB database instance

Source: Internet
Author: User
This article describes how Python uses the Berkeley DB database and shares it for everyone's reference.

The implementation method is as follows:

Try: From   bsddb import db except Importerror: from   bsddb3 Import DB Print db. Db_version_string #检测是否有bsddb包  def irecords (curs):   record = Curs.first () while   record:     yield record     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 ', ' at ') #加入数据库 the_same_db.put (' for ', ' Change the data ') # Change database data for key, Irecords (The_same_db.cursor ()):   print Key,data the_same_db.close ()

The results of the operation are as follows:

Berkeley DB 4.7.25: (May) example 3some 0word 1for 2*********************************************************** *example 3some 0word 1for Change the Dataskidoo 23

Here is a summary of 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. Inserting or modifying data in a database

Adb.put (' Skidoo ', ' all ') #加入数据库adb. Put (' for ', ' Change the data ') #改变数据库的数据

4. Close the database

Adb.close ()

Hopefully 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.