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.