Python uses the Berkeley DB database instance, pythonberkeley
This article describes how to use the Berkeley DB database in python.
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.
Is Berkeley DB included in RedHat 60?
Yes, you don't need to install it any more. You can use it directly. Here is an example.
#-*-Encoding: gb2312-*-import OS, sys, stringimport bsddb, timehome = "db_home" filename = "test. db "try: # create the home Directory OS. mkdir (home) failed T: pass # create a database environment dbenv = bsddb. db. DBEnv () # Open the database environment dbenv. open (home, bsddb. db. DB_CREATE | bsddb. db. DB_INIT_MPOOL) # create a database object d = bsddb. db. DB (dbenv) # Open the database. The second parameter specifies the data access method. # btree is bsddb. db. DB_BTREE, hash is bsddb. db. DB_HASH # The queu is bsddb. db. DB_QUEUE, recno is bsddb. db. DB_RECNOd.open (filename, bsddb. db. DB_BTREE, bsddb. db. DB_CREATE, 0666) # insert a piece of data. Note that the keys of queue and recno cannot be strings, but they should be numbers d. put ('test1', 'zhaowei') print d. items () # Close. Data is written back to file d. close () dbenv. close ()
In the bsddb module of python, both the key and value can be one, and both are string types. This is different from the Berkeley DB Design.
{"Key": [3, 2.9, True]} Does this work?