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