Python uses Berkeley DB database instance _python

Source: Internet
Author: User

This article describes the Python use of Berkeley DB Database method, share for everyone to reference.

The implementation methods are 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 ', ', ') #加入数据库 
the_same_db.put (' for ') , ' Change the data '] #改变数据库的数据 
for key, data in 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, 2008)
Example 3
some 0
word 1 for
2
**********************************
Example 3
some 0
word 1 for change the
data
Skidoo 23

Here is a summary of the steps to be done:

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 ', ', ') #加入数据库
adb.put (' for ', ' Change the data ') #改变数据库的数据

4. Close the database

Adb.close ()

I hope this article will help you with your 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.