Question

I am storing a values in form of dictionary in the database. Now how do i view this data which are getting stored in this database ?? I want to list all the data stored in database. I am using Berkeley db. And using dictionary i am storing data

#!/usr/bin/python

import bsddb
import cgi

form = cgi.FieldStorage()

print "Content-type:text/html\n"
Fname = form.getvalue('firstname', '')
Lname = form.getvalue('lastname', '')
Age = form.getvalue('age', 0)
Gender = form.getvalue('gender', '')

#print Fname, Lname, Age 

db = bsddb.hashopen("/home/neeraj/public_html/database/mydb.db","w")
db['FirstName'] = Fname  
db['LastName'] = Lname
db['Age'] = Age 
db['Gender'] = Gender
db.close()
db = bsddb.hashopen("/home/neeraj/public_html/database/mydb.db","r")
#db = bsddb.hashopen("/home/neeraj/public_html/database/mydb.db")
print db['FirstName'], db['LastName'], db['Age'], db['Gender']
db.close()
Was it helpful?

Solution

The same way you'd view data in a dict: by iterating it.

Some of the database modules, including bsddb, don't have an __iter__ method, but they all have keys. So:

db = bsddb.hashopen("/home/neeraj/public_html/database/mydb.db","r")
for key in db.keys():
    print('{}: {}'.format(key, db[key]))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top