#!/usr/bin/env python#-*- coding:utf-8 -*-#Author:Huanglinsheng
importos#查詢方式一:select * from data_staff.txt where age >= 22#查詢方式二:select * from data_staff.txt where dept = "IT"#查詢方式三:select * from data_staff.txt where enroll_date like "2013"
defSearch():
data=input("Input your select info:")
data= data.split(' ')
con= data[7]
asp= data[5]
count=0
with open('data_staff.txt','r',encoding='utf-8') as f:if asp == 'age':for line inf:if int(line.split(',')[2]) >=int(con):print(line)
count+=1
elif asp == 'dept':for line inf:if line.split(',')[4] incon:print(line)
count+=1
else:for line inf:if line.split(',')[5].split('-')[0] incon:print(line)
count+=1
print('查詢結束,共查到符合條件的信息 %d 條' %count)#添加語法: name,age,phone,dept,enroll-date
defAdd():
data= input("請輸入要添加員工的信息:")
list_data= data.strip().split(',')
list_all=[]
f= open('data_staff.txt','r+')for line inf:
list_all.append(line.strip().split(',')[3])if list_data[2] inlist_all:print("該用戶已經存在!!")else:for line inf:
f.write(line)
staff_id= str(len(list_all) + 1)
list_data.insert(0,str(staff_id))
f.write('\n')
f.write(','.join(list_all))
f.close()print("添加成功")#刪除語法:delete from staff_table where staff_id = 12
defDelete():
staff_id= input("輸入您要刪除員工的Staff_id:")
staff_id= staff_id.strip().split(' ')[6]
f= open('data_staff.txt','r')
f1= open('new_data_staff.txt','w')for line inf:
in_list= line.split(',')if in_list[0]
f1.write(line)elif in_list[0] >staff_id:
in_list[0]= str(int(in_list[0]) - 1)
f1.write(','.join(in_list))else:continuef.close()
f1.close()
os.remove("data_staff.txt")
os.rename('new_data_staff.txt','data_staff')print("刪除成功!!")#修改請輸入(注意空格和沒有引號):UPDATE staff_table SET dept = IT where dept = 運維
defChange():
data= input("請輸入你要修改的信息:")
old= data.split(' ')[5]
new= data.split(' ')[9]
f= open('data_staff.txt','r',encoding='utf-8')
f1= open('new_data_staff','w',encoding='utf-8')for line inf:if old inline:
line=line.replace(old,new)
f1.write(line)
f.close()
f1.close()
os.remove('data_staff')
os.rename('new_data_staff', 'data_staff')print('修改成功')
msg_dict={'1':Search,'2':Add,'3':Delete,'4':Change,'5':'退出'}whileTrue:print("""1:查詢
2:添加
3:刪除
4:修改
5:退出""")
choice= input("Input your choice:")if choice not inmsg_dict:print("Input error!Pls Input one more time.")continue
if int(choice) == 5:
exit()else:
msg_dict[choice]()