-
Notifications
You must be signed in to change notification settings - Fork 5
/
bdb_ex.py
42 lines (33 loc) · 1.04 KB
/
bdb_ex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/python
from bsddb3 import db # the Berkeley db data base
# Part 1: Create database and insert 4 elements
#
filename = 'fruit'
# Get an instance of BerkeleyDB
fruitDB = db.DB()
# Create a database in file "fruit" with a Hash access method
# There are also, B+tree and Recno access methods
fruitDB.open(filename, None, db.DB_HASH, db.DB_CREATE)
# Print version information
print '\t', db.DB_VERSION_STRING
# Insert new elements in database
fruitDB.put("apple","red")
fruitDB.put("orange","orange")
fruitDB.put("banana","yellow")
fruitDB.put("tomato","red")
# Close database
fruitDB.close()
# Part 2: Open database and write its contents out
#
fruitDB = db.DB()
# Open database
# Access method: Hash
# set isolation level to "dirty read (read uncommited)"
fruitDB.open(filename, None, db.DB_HASH, db.DB_DIRTY_READ)
# get database cursor and print out database content
cursor = fruitDB.cursor()
rec = cursor.first()
while rec:
print rec
rec = cursor.next()
fruitDB.close()