Python programming practice---simple database application


Use python to add and search databases, take personnel addition and search as an example

Based on python3.6


code



import sys,shelve 


def store_person(db):
    number=input("Please enter this person's ID:")
    person={}
    person['name']=input("Please enter this person's name:")
    person[' age']=input("Please enter this person's age:")
    person['tel']=input("Please enter this person's phone number:")
    db[number]=person
    
    
def search_person(db):
    num=input ("Please enter the ID you want to find:")
    middle=input("You want to find name or age or tel:")
    middle=middle.strip().lower() #Process the input string and remove the redundant Spaces and capitalize strings to lowercase
    print(middle.capitalize()+":",db[num][middle]) #capitalize function capitalizes the first character of a string
    
    
def help():
    print("Related operations And description: ")
    print('store ',"Enter personnel information")
    print("search ","Find personnel information")
    print("quit","Quit")
    print("help","View help instructions")
    
def main():
    datebase=shelve.open('G:\\人员数据库')
    help()
    a=1
    try:
        while a!='quit':
            a=input("请输入相关操作:")
            if a=='store':
                store_person(datebase)
            elif a=='search':
                search_person(datebase)
            elif a=='help':
                help()
                
    finally:
        datebase.close()
            
if __name__=='__main__':
    main()
            
 


Run the screenshot:









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325437973&siteId=291194637