标签:影响 语句 taf class table 增删改 输入 pytho ===
README
现要求你写一个简单的员工信息增删改查程序,需求如下:
当然此表你在文件存储时可以这样表示
1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,28,13451024608,HR,2015-01-07
3,Rain Wang,21,13451054608,IT,2017-04-01
4,Mack Qiao,44,15653354208,Sales,2016-02-01
5,Rachel Chen,23,13351024606,IT,2013-03-16
6,Eric Liu,19,18531054602,Marketing,2012-12-01
7,Chao Zhang,21,13235324334,Administration,2011-08-08
8,Kevin Chen,22,13151054603,Sales,2013-04-01
9,Shit Wen,20,13351024602,IT,2017-07-03
10,Shanshan Du,26,13698424612,Operation,2017-07-02
1.可进行模糊查询,语法至少支持下面3种查询语法:
find name,age from staff_table where age > 22
find * from staff_table where dept = "IT"
find * from staff_table where enroll_date like "2013"
2.可创建新员工纪录,以phone做唯一键(即不允许表里有手机号重复的情况),staff_id需自增
语法: add staff_table Alex Li,25,134435344,IT,2015-10-29
3.可删除指定员工信息纪录,输入员工id,即可删除
语法: del from staff where id=3
4.可修改员工信息,语法如下:
UPDATE staff_table SET dept="Market" WHERE dept = "IT" 把所有dept=IT的纪录的dept改成Market
UPDATE staff_table SET age=25 WHERE name = "Alex Li" 把name=Alex Li的纪录的年龄改成25
5.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。
注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!
二、程序目录结构
├──员工管信息管理系统
│
├—— README
│
├── staff_table # 员工信息表数据
│
├—— staff.py # 主程序
三、程序效果图
四、功能验证
1、用户登录验证 (用户名:admin 密码:123456)
验证目的:当用户未登录时,调用增删改查功能时,都需要身份验证
2、增加员工信息
增加员工时,staff_id 自增,phone 不可重复
3、删除员工信息
现在删除刚才增加的员工信息,staff_id 为删除条件,删除后,staff_id 自动排序
4、修改员工信息
修改方式:
只有age和dept能被修改,修改语句如下(请任选以下修改语句):
a、UPDATE staff_table SET dept="Market" WHERE dept = "IT"
b、UPDATE staff_table SET age=25 WHERE name = "Alex Li"
5、查询员工信息
本系统支持以下三种查询方法(查询条件分别为:age、dept、enroll_date):
a、find name,age from staff_table where age > 22
b、find * from staff_table where dept = "IT"
c、find * from staff_table where enroll_date like "2013"
六、程序代码:
import sys status_flag = False # 标记用户登录状态 0代表为登录,1代表已登录 # 打印菜单 def menu(): info = ‘‘‘ ==========员工信息表========== 1、增加员工信息 2、删除员工信息 3、修改员工信息 4、查询员工信息 5、退出 ============================= ‘‘‘ print(info) # 用户登录验证器 def login(func): def inner(): global status_flag # 声明全局变量 if status_flag: func() else: user = ‘admin‘ passwd = ‘123456‘ print("请先登录管理员账户!") username = input("用户名: ") password = input("密码:") if username == user and password == passwd: print("Welcome!", username) status_flag = True func() else: print("Logon failure!") sys.exit(1) return inner #将文件信息转换成[[],[]...]形式的列表 def change(): staff_list = [] with open(‘staff_table‘,‘r‘,encoding=‘utf-8‘) as staff_f: for i in staff_f: i = i.strip(‘\n‘).split(‘,‘) staff_list.append(i) return staff_list #更新文件信息 def update_file(staff_list): with open(‘staff_table‘, ‘w‘,encoding=‘utf-8‘) as staff_f: for i in staff_list: i = ‘,‘.join(i)+‘\n‘ staff_f.write(i) #增加函数 @login def add(): print(‘=*=‘*20) print("添加的内容格式请按照: Alex Li,25,13651054608,IT,2015-10-29") print(‘=*=‘*20) staff_list = change() addInfo = input("add staff_table ").strip() addInfo_list = addInfo.split(‘,‘) if len(addInfo_list) == 5: phone_list = [] for i in staff_list: phone_list.append(i[3]) if addInfo_list[2] in phone_list: print("phone 号码已存在,此次添加无效!") else: max_id = int(staff_list[-1][0]) add_id = max_id + 1 addInfo_list.insert(0,add_id) addInfo_list[0] = str(addInfo_list[0]) staff_list.append(addInfo_list) update_file(staff_list) print("以下员工信息已添加成功!") print(‘,‘.join(addInfo_list)) else: print("%s为无效的输入!"%addInfo) #删除函数 @login def delete(): print(‘=*=‘ * 20) print("请输入您想要删除员工的staff_id") staff_list = change() ID = input("del from staff where id = ").strip() if ID.isdigit(): ID = int(ID) del_list = [] max_id = int(staff_list[-1][0]) # 获取最后一行数据的序号 if ID <= max_id: for i,k in enumerate(staff_list): if int(k[0]) == ID: del_list.append(k) del staff_list[i] if len(del_list) == 0: print("staff_ID为%d的员工不存在..."%ID) else: print("以下员工已被删除成功!") for i in del_list: i[0] = str(i[0]) print(‘,‘.join(i)) update_file(staff_list) else: print("staff_id:%d 不存在!"%ID) else: print("%s为无效的输入..."%ID) #修改函数 @login def update(): update_info = ‘‘‘ =*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*= 只有age和dept能被修改,修改语句如下(请任选以下修改语句): 1、UPDATE staff_table SET dept="Market" WHERE dept = "IT" 2、UPDATE staff_table SET age=25 WHERE name = "Alex Li" ‘‘‘ print(update_info) staff_list = change() update_choice = input("您想根据什么修改信息?(name or dept):").strip() if update_choice == ‘dept‘: old_dept = input("请输入修改条件dept:").strip() new_dept = input("请输入修改之后dept的值:").strip() updateDept_list = [] for i in staff_list: if i[4] == old_dept: i[4] = new_dept updateDept_list.append(i) if len(updateDept_list) > 0: print("以下信息已经修改成功!") for i in updateDept_list: print(‘,‘.join(i)) update_file(staff_list) else: print("%s不存在!"%old_dept) elif update_choice == ‘name‘: Name = input("请输入修改条件name的值:").strip() new_age = input("请输入修改后的age:").strip() updateAge_list = [] for i,k in enumerate(staff_list): if k[1] == Name: staff_list[i][2] = new_age updateAge_list.append(k) if len(updateAge_list) > 0: print("以下信息已经修改成功!") for i in updateAge_list: print(‘,‘.join(i)) update_file(staff_list) else: print("%s不存在!"%Name) else: print("%s为无效的输入..."%update_choice) #查询函数 @login def find(): find_info = ‘‘‘ =*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*= 本系统支持以下三种查询方法(查询条件分别为:age、dept、enroll_date): 1、find name,age from staff_table where age > 22 2、find * from staff_table where dept = "IT" 3、find * from staff_table where enroll_date like "2013" ‘‘‘ print(find_info) staff_list = change() find_choice = input("您想使用哪种方法查询员工信息(请填写查询条件):").strip() if find_choice == ‘age‘: find_age = input("find name,age from staff_table where age > ").strip() if find_age.isdigit(): find_age = int(find_age) findAge_list = [] for i in staff_list: i[2] = int(i[2]) if i[2] > find_age: findAge_list.append(i) if len(findAge_list) > 0: print("符合条件的信息如下:") for i in findAge_list: print("%s,%s"%(i[1],i[2])) else: print("无age大于%s的员工信息!"%find_age) else: print("%s非数字,输入无效!"%find_age) elif find_choice == ‘dept‘: find_dept = input("find * from staff_table where dept = ") findDept_list = [] for i in staff_list: if i[4] == find_dept: findDept_list.append(i) if len(findDept_list) > 0: print("符合条件的信息如下:") for i in findDept_list: print(‘,‘.join(i)) else: print("无dept为%s的员工信息!" % find_dept) elif find_choice == ‘enroll_date‘: findEnroll_date = input("find * from staff_table where enroll_date like ") findEnroll_date_list = [] for i in staff_list: i_date = i[5] i_date = i_date.split(‘-‘)[0] if i_date == findEnroll_date: findEnroll_date_list.append(i) if len(findEnroll_date_list) > 0: print("符合条件的信息如下:") for i in findEnroll_date_list: print(‘,‘.join(i)) else: print("Enroll_date为%s 的信息不存在!"%findEnroll_date) else: print("%s为无效的输入!"%find_choice) def main(): while True: menu() choice = input("请选择:").strip() if choice == ‘1‘: add() elif choice == ‘2‘: delete() elif choice == ‘3‘: update() elif choice == ‘4‘: find() elif choice == ‘5‘: sys.exit("logout......") else: print("无效的输入!") main()
标签:影响 语句 taf class table 增删改 输入 pytho ===
原文地址:https://www.cnblogs.com/alvin-jie/p/8892030.html