Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3311142
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:50:32+00:00 2026-05-17T21:50:32+00:00

I want to store an integer key in shelve. But when I try to

  • 0

I want to store an integer key in shelve. But when I try to store integer key in shelve it give me an error

Traceback (most recent call last):
  File "./write.py", line 12, in 
    data[id] = {"Id": id, "Name": name}
  File "/usr/lib/python2.5/shelve.py", line 124, in __setitem__
    self.dict[key] = f.getvalue()
  File "/usr/lib/python2.5/bsddb/__init__.py", line 230, in __setitem__
    _DeadlockWrap(wrapF)  # self.db[key] = value
  File "/usr/lib/python2.5/bsddb/dbutils.py", line 62, in DeadlockWrap
    return function(*_args, **_kwargs)
  File "/usr/lib/python2.5/bsddb/__init__.py", line 229, in wrapF
    self.db[key] = value
TypeError: Integer keys only allowed for Recno and Queue DB's

My Code :

#!/usr/bin/python

import shelve

data = shelve.open("data.txt")

ans = 'y'
while ans == "y":
    id = input("Enter Id : ")
    name = raw_input("Enter name : ")

    data[id] = {"Id": id, "Name": name}

    ans = raw_input("Do you want to continue (y/n) ? : ")

data.close()

Is something wrong in my program or shelve does not supports integer keys at all ?


Edit 1 :

In program I am trying to store a dictionary of Id and Name inside another dictionary with Id as a key. And then trying to store it in a file.

Do I need to use Recno or Queue DB’s along with shelve? I am a beginner and things are confusing.

Let me know if I am not clear with my question.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:50:32+00:00Added an answer on May 17, 2026 at 9:50 pm

    The shelve module uses an underlying database package (such as dbm, gdbm or bsddb) .

    A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings. The examples section gives you the proof.

    This should work. Here’s what I do in my code –

    import shelve
    
    #Create shelve
    s = shelve.open('test_shelf.db')
    try:
        s['key1'] = { 'int': 10, 'float':9.5, 'string':'Sample data' }
    finally:
        s.close()
    
    #Access shelve
    s = shelve.open('test_shelf.db')
    try:
        existing = s['key1']
    finally:
        s.close()
    print existing
    

    UPDATE: You could try pickle module. It is not a key-value database but you can always build your data structure as a key-value pairs and then send it to pickle –

    If you have an object x, and a file object f that’s been opened for writing, the simplest way to pickle the object takes only one line of code

    pickle.dump(x, f)
    

    To unpickle the object again, if f is a file object which has been opened for reading:

    x = pickle.load(f)
    

    I hear cPickle is a lot faster than pickle. You can try this if you have lot of data to store.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store a large number of sound files in a database, but
I want to store the current URL in a session variable to reference the
I want to store the data returned by $_SERVER[REMOTE_ADDR] in PHP into a DB
I want to store a a c# DateTimeOffset value in a SQL Server 2005
I want to store values in a bunch of currencies and I'm not too
I want to store a large result set from database in memory. Every record
I want to store a very large amount of vector data on a server
I want to store a URL prefix in an Windows environment variable. The ampersands
I want to store a list of the following tuples in a compressed format
I'm working on a project and I want to store some easily enumerated information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.