database based on shelve file : shelve « Database « Python Tutorial






import sys, shelve

def store_person(db):
    pid = raw_input('Enter unique ID number: ')
    person = {}
    person['name']  = raw_input('Enter name: ')
    person['age']  = raw_input('Enter age: ')
    person['phone'] = raw_input('Enter phone number: ')

    db[pid] = person

def lookup_person(db):
    pid = raw_input('Enter ID number: ')
    field = raw_input('What would you like to know? (name, age, phone)  ')
    field = field.strip().lower()
    print field.capitalize() + ':', db[pid][field]

def enter_command():
    cmd = raw_input('Enter command (? for help): ')
    cmd = cmd.strip().lower()
    return cmd

database = shelve.open('c:\\database.dat')
store_person(database)
lookup_person(database)
database.close()








15.5.shelve
15.5.1.object returned by shelve.open is not an ordinary mapping
15.5.2.Storing Objects in a Shelve File
15.5.3.Changing Objects in a Shelve File
15.5.4.Writing to shelve file.
15.5.5.Reading a shelve file.
15.5.6.database based on shelve file