A database with integer key using pickle

import anydbm
db = anydbm.open('integer.db', 'c')
import pickle
# A limitation of anydbm is that the keys and values have to be strings.
#  If you try to use any other type, you get an error

# The pickle module can help. It translates almost any type of object into a string suitable
# for storage in a database, and then translates strings back into objects.
k = 1
f=pickle.dumps(k)
db[f] = 'Babu'
print 'Value:',db[f]
print 'Key:',f
db.close()
r=pickle.loads(f)
print 'Key Value back:',r

No comments: