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.