# database.py
import sys, shelve
def store_person(db):
"""
讓用戶輸入數(shù)據(jù)并將其存儲(chǔ)到shelf對(duì)象中
"""
pid = input('Enter unique ID number: ')
person = {}
person['name'] = input('Enter name: ')
person['age'] = input('Enter age: ')
person['phone'] = input('Enter phone number: ')
db[pid] = person
def lookup_person(db):
"""
讓用戶輸入ID和所需的字段,并從shelf對(duì)象中獲取相應(yīng)的數(shù)據(jù)
"""
pid = input('Enter ID number: ')
field = input('What would you like to know? (name, age, phone) ')
field = field.strip().lower()
print(field.capitalize() + ':', db[pid][field])
def print_help():
print('The available commands are:')
print('store : Stores information about a person')
print('lookup : Looks up a person from ID number')
print('quit : Save changes and exit')
print('? : Prints this message')
def enter_command():
cmd = input('Enter command (? for help): ')
cmd = cmd.strip().lower()
return cmd
def main():
database = shelve.open(r'D:\DataScience\2019\02\database.dat') # 你可能想修改這個(gè)名稱
try:
while True:
cmd = enter_command()
if cmd == 'store':
store_person(database)
elif cmd == 'lookup':
lookup_person(database)
elif cmd == '?' or cmd == 'help':
print_help()
elif cmd == 'quit':
return
finally:
database.close()
if __name__ == '__main__': main()
代碼清單 10-8 簡(jiǎn)單的數(shù)據(jù)庫(kù)程序應(yīng)用
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 夏季是做背部最佳時(shí)期。很多人不知道,做后背是為了什么。其實(shí)做后背,做的是五臟六腑,調(diào)的是我們的心,肝,脾,肺,腎。...
- 剛才吃早點(diǎn) 在我前面的是個(gè)高中生 “吃啥” “給我來(lái)個(gè)肉夾饃 來(lái)碗豆腐腦 ” “再要碗雜碎面” “我主要是不知道...
- 《隋書(shū)》有云:“萬(wàn)濤間遠(yuǎn)而望之,蟠旋蜿蜒,若虬浮水中,故因以名琉虬?!边@就是琉球之名的來(lái)源。 歷史上琉球王國(guó)一直是...