项目:员工信息增删改查程序
Posted 战争热诚
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目:员工信息增删改查程序相关的知识,希望对你有一定的参考价值。
现要求写一个简单的员工信息增删改查程序,需求如下:
当然此表你在文件存储时可以这样表示
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.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。
注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!
一:题目思路图:
二,程序概述:
**概述:** 本次作业文件夹一共包含了以下4个文件: 流程图:员工信息表思路流程图 程序文件: run_program.py 用户信息文件:staff_info.txt 程序说明文件:README.md **一,程序功能** 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.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。 **二,部分变量说明** prompt_func() 欢迎登录的函数名 initial_employee_information() 初始化员工信息的函数名 find_func() 查找函数名 add_func() 添加函数名 del_func() 删除函数名 update_func() 更新函数名 main() 主函数 staff_infofile 读取文件后,存放文件内容的变量 user_input 用户输入 core_message 用户添加的信息内容 DATA_STAFF 初始化数据,定义成常量 after_update_name 更新后的名称 after_update_content 更新后的内容 before_update_name 更新前的名称 before_update_content 更新前的内容 **三,运行代码** 本程序的开发环境是python3.x 运行后,根据控制台显示的提示信息执行
三,主程序:
#_*_coding:utf-8_*_ def prompt_func(): return (\'\'\' 欢迎来到员工信息查询系统! 操作选项: 1、模糊查询员工信息 2、新增员工信息 3、删除指定员工信息 4、修改员工信息 5、quit返回上一级 \'\'\') def initial_employee_information(): \'\'\' 初始化员工信息数据,即把员工信息读到内存里面 :return:{\'id\': [\'1\', \'2\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\', \'10\'], \'name\': [\'Alex Li\', \'Jack Wang\', \'Mack Qiao\', \'Rachel Chen\', \'Eric Liu\', \'Chao Zhang\', \'Kevin Chen\', \'Shit Wen\', \'Shanshan Du\'], \'age\': [\'22\', \'28\', \'44\', \'23\', \'19\', \'21\', \'22\', \'20\', \'26\'], \'phone\': [\'13651054608\', \'13451024608\', \'15653354208\', \'13351024606\', \'18531054602\', \'13235324334\', \'13151054603\', \'13351024602\', \'13698424612\'], \'depart\': [\'IT\', \'HR\', \'Sales\', \'IT\', \'Marketing\', \'Administration\', \'Sales\', \'IT\', \'Operation\'], \'enrolled_date\': [\'2013-04-01\\n\', \'2015-01-07\\n\', \'2016-02-01\\n\', \'2013-03-16\\n\', \'2012-12-01\\n\', \'2011-08-08\\n\', \'2013-04-01\\n\', \'2017-07-03\\n\', \'2017-07-02\']} \'\'\' data_staff = {} staff_list = [\'id\', \'name\', \'age\', \'phone\', \'depart\', \'enrolled_date\'] for i in staff_list: data_staff[i] = [] # print_log(data_staff) staff_infofile = open(\'staff_info.txt\',\'r+\',encoding=\'utf-8\') for line in staff_infofile: staff_id, staff_name, staff_age, staff_phone, staff_depart, staff_date = line.split(\',\') data_staff[\'id\'].append(staff_id) data_staff[\'name\'].append(staff_name) data_staff[\'age\'].append(staff_age) data_staff[\'phone\'].append(staff_phone) data_staff[\'depart\'].append(staff_depart) data_staff[\'enrolled_date\'].append(staff_date) staff_infofile.close() return data_staff DATA_STAFF = initial_employee_information() def find_func(): while True: print(\'\'\' *************************************************** 命令行示例: find name age where age > 20 find * from staff_table where dept IT find * from staff_table where enroll_date like 2013 *************************************************** \'\'\') user_input = input("请输入您要查询员工信息的正确的语法(如果想返回上一层,请按q):").split() for index,age in enumerate(DATA_STAFF[\'age\']): if \'>\' in user_input: if age > user_input[-1]: print(DATA_STAFF[\'name\'][index],DATA_STAFF[\'age\'][index]) elif \'<\' in user_input: if age < user_input[-1]: print(DATA_STAFF[\'name\'][index],DATA_STAFF[\'age\'][index]) elif \'=\' in user_input: if age == user_input[-1]: print(DATA_STAFF[\'name\'][index],DATA_STAFF[\'age\'][index]) for index,depart in enumerate(DATA_STAFF[\'depart\']): if depart in user_input: print(DATA_STAFF[\'id\'][index], DATA_STAFF[\'name\'][index], DATA_STAFF[\'age\'][index],DATA_STAFF[\'phone\'][index], DATA_STAFF[\'depart\'][index], DATA_STAFF[\'enrolled_date\'][index]) for index,enrolled_date in enumerate(DATA_STAFF[\'enrolled_date\']): enrolled_date =enrolled_date.split(\'-\')[0] if enrolled_date in user_input and \'like\' in user_input: print(DATA_STAFF[\'id\'][index], DATA_STAFF[\'name\'][index], DATA_STAFF[\'age\'][index],DATA_STAFF[\'phone\'][index], DATA_STAFF[\'depart\'][index], DATA_STAFF[\'enrolled_date\'][index]) if user_input == \'q\'.split(): break def add_func(): while True: print(\'\'\' *************************************************************** 员工录入示例:add staff_table Alex Li,25,134435344,IT,2015-10-29 *************************************************************** \'\'\') staff_infofile = open(\'staff_info.txt\',\'a+\',encoding=\'utf-8\') user_input = input("请输入您要增加员工信息的正确的语法(如果想返回上一层,请按q):\\n").split(\'staff_table\') if user_input == \'q\'.split(): break core_message = \',\'.join(user_input) core_message =core_message.split(\',\')[1:] STAFF_ID = int(DATA_STAFF[\'id\'][-1]) +1 DATA_STAFF[\'id\'].append(STAFF_ID) for iphone in DATA_STAFF[\'phone\']: if core_message[2] ==iphone: print("手机不允许重复,请重新添加") add_func() else: pass staff_infofile.write(\'\\n\' + str(STAFF_ID) + \',\' + \',\'.join(core_message)) staff_infofile.close() print(\'\\033[1;31m 影响了1条记录 \\033[0m\') def del_func(): print(\'\'\' *********************************** 删除示例:del from staff where id=3 *********************************** \'\'\') del_staffid = input("请输入您要删除的员工id的语法:\\n ") if len(del_staffid) ==26 or len(del_staffid) ==27: del_staffid = del_staffid.split(\'=\') count = 1 del_left, del_right = del_staffid if del_right in DATA_STAFF[\'id\']: staff_index = DATA_STAFF[\'id\'].index(del_right) staff_infofile = open(\'staff_info.txt\', \'w\', encoding=\'utf_8\') print(\'\\033[31;1m员工 \' + DATA_STAFF[\'name\'][staff_index] + \' 已经删除\\033[0m\') del DATA_STAFF[\'id\'][staff_index] del DATA_STAFF[\'name\'][staff_index] del DATA_STAFF[\'age\'][staff_index] del DATA_STAFF[\'phone\'][staff_index] del DATA_STAFF[\'depart\'][staff_index] del DATA_STAFF[\'enrolled_date\'][staff_index] while True: staff_wr = DATA_STAFF[\'id\'][count] + \',\' + DATA_STAFF[\'name\'][count] + \',\'\\ + DATA_STAFF[\'age\'][count] + \',\' + DATA_STAFF[\'phone\'][count] + \',\'\\ + DATA_STAFF[\'depart\'][count] + \',\' + DATA_STAFF[\'enrolled_date\'][count] staff_infofile.write(staff_wr) count += 1 if count == len(DATA_STAFF[\'id\']): break staff_infofile.close() else: print("\\033[31;1m员工信息表中无此员工的信息,请重新输入\\033[0m") del_func() else: print("\\033[31;1m输入的语法有误,请重输!\\33[0m") del_func() print(\'\\033[1;31m 影响了1条记录 \\033[0m\') def update_func(): print(\'\'\' ************************************************************* 示例:UPDATE staff_table SET dept="Market" WHERE dept = "IT" UPDATE staff_table SET age=25 WHERE name = "Alex Li" ************************************************************* \'\'\') update_staff = input("请输入您要更新的员工信息的语法:\\n ") user_update = update_staff.split(\'SET\') update_staff_left,update_staff_right = user_update user_update_finally = user_update[-1].strip().split(\'WHERE\') after_update = user_update_finally[0] before_update = user_update_finally[1] after_update_name,after_update_content = after_update.split(\'=\') before_update_name, before_update_content = before_update.split(\'=\') if after_update_name.strip() == before_update_name.strip(): for dept in DATA_STAFF[\'depart\']: if dept == eval(before_update_content): DATA_STAFF[\'depart\'][DATA_STAFF[\'depart\'].index(eval(before_update_content))] \\ = eval(after_update_content) count = 0 staff_infofile = open(\'staff_info.txt\', \'w\', encoding=\'utf_8\') while True: staff_wr = DATA_STAFF[\'id\'][count] + \',\' + DATA_STAFF[\'name\'][count] + \',\' \\ + DATA_STAFF[\'age\'][count] + \',\' + DATA_STAFF[\'phone\'][count] + \',\' \\ + DATA_STAFF[\'depart\'][count] + \',\' + DATA_STAFF[\'enrolled_date\'][count] staff_infofile.write(staff_wr) count += 1 if count == len(DATA_STAFF[\'id\']): break staff_infofile.close() print(\'\\033[1;31m 影响了3条记录 \\033[0m\') else: for index,name in enumerate(DATA_STAFF[\'name\']): if name.strip() == eval(before_update_content).strip(): DATA_STAFF[\'age\'][index] = eval(after_update_content) else: pass count = 0 staff_infofile = open(\'staff_info.txt\', \'w\', encoding=\'utf_8\') while True: staff_wr = str(DATA_STAFF[\'id\'][count]) + \',\' + DATA_STAFF[\'name\'][count] + \',\' \\ + str(DATA_STAFF[\'age\'][count]) + \',\' + str(DATA_STAFF[\'phone\'][count]) + \',\' \\ + DATA_STAFF[\'depart\'][count] + \',\' + DATA_STAFF[\'enrolled_date\'][count] staff_infofile.write(staff_wr) count += 1 if count == len(DATA_STAFF[\'id\']): break staff_infofile.close() print(\'\\033[1;31m 影响了1条记录 \\033[0m\') def main(): while True: print(prompt_func()) user_input = input("请输入要执行操作的序号>> ") if user_input == \'1\': print("-------------欢迎进入模糊查询员工信息界面----------------") find_func() elif user_input == \'2\': print("-------------欢迎进入新增员工信息界面----------------") add_func() elif user_input == \'3\': print("-------------欢迎进入删除指定员工信息界面----------------") del_func() elif user_input == \'4\': print("-------------欢迎进入修改员工信息界面----------------") update_func() else: print("\\033[31;1m输入的信息有误,请重输!\\33[0m") if __name__ == \'__main__\': main()
更新版的:
#_*_coding:utf-8_*_ def prompt_func(): return (\'\'\' 欢迎来到员工信息查询系统! 操作选项: 1、模糊查询员工信息 2、新增员工信息 3、删除指定员工信息 4、修改员工信息 \'\'\') def initial_employee_information(): \'\'\' 初始化员工信息数据,即把员工信息读到内存里面 :return:{\'id\': [\'1\', \'2\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\', \'10\'], \'name\': [\'Alex Li\', \'Jack Wang\', \'Mack Qiao\', \'Rachel Chen\', \'Eric Liu\', \'Chao Zhang\', \'Kevin Chen\', \'Shit Wen\', \'Shanshan Du\'], \'age\': [\'22\', \'28\', \'44\', \'23\', \'19\', \'21\', \'22\', \'20\', \'26\'], \'phone\': [\'13651054608\', \'13451024608\', \'15653354208\', \'13351024606\', \'18531054602\', \'13235324334\', \'13151054603\', \'13351024602\', \'13698424612\'], \'depart\': [\'IT\', \'HR\', \'Sales\', \'IT\', \'Marketing\', \'Administration\', \'Sales\', \'IT\', \'Operation\'], \'enrolled_date\': [\'2013-04-01\\n\', \'2015-01-07\\n\', \'2016-02-01\\n\', \'2013-03-16\\n\', \'2012-12-01\\n\', \'2011-08-08\\n\', \'2013-04-01\\n\', \'2017-07-03\\n\', \'2017-07-02\']} \'\'\' data_staff = {} staff_list = [\'id\', \'name\', \'age\', \'phone\', \'depart\', \'enrolled_date\'] for i in staff_list: data_staff[i] = [] staff_infofile = open(\'staff_info.txt\',\'r+\',encoding=\'utf-8\') for line in staff_infofile: staff_id, staff_name, staff_age, staff_phone, staff_depart, staff_date = line.split(\',\') data_staff[\'id\'].append(staff_id) data_staff[\'name\'].append(staff_name) data_staff[\'age\'].append(staff_age) data_staff[\'phone\'].append(staff_phone) data_staff[\'depart\'].append(staff_depart) data_staff[\'enrolled_date\'].append(staff_date) staff_infofile.close() return data_staff DATA_STAFF = initial_employee_information() def find_func(): while True: print(\'\'\' *************************************************** 命令行示例: find name age where age > 20 find * from staff_table where dept IT find * from staff_table where enroll_date like 2013 *************************************************** \'\'\') user_input = input("请输入您要查询员工信息的正确的语法(如果想返回上一层,请按q):").split() for index,age in enumerate(DATA_STAFF[\'age\']): if \'>\' in user_input: if age > user_input[-1]: print(DATA_STAFF[\'name\'][index],DATA_STAFF[\'age\'][index]) elif \'<\' in user_input: if age < user_input[-1]: print(DATA_STAFF[\'name\'][index],DATA_STAFF[\'age\'][index]) elif \'=\' in user_input: if age == user_input[-1]: print(DATA_STAFF[\'name\'][index],DATA_STAFF[\'age\'][index]) for index,depart in enumerate(DATA_STAFF[\'depart\']): if depart in user_input: print(DATA_STAFF[\'id\'][index], DATA_STAFF[\'name\'][index], DATA_STAFF[\'age\'][index],DATA_STAFF[\'phone\'][index], DATA_STAFF[\'depart\'][index], DATA_STAFF[\'enrolled_date\'][index]) for index,enrolled_date in enumerate(DATA_STAFF[\'enrolled_date\']): enrolled_date =enrolled_date.split(\'-\')[0] if enrolled_date in user_input and \'like\' in user_input: print(DATA_STAFF[\'id\'][index], DATA_STAFF[\'name\'][index], DATA_STAFF[\'age\'][index],DATA_STAFF[\'phone\'][index], DATA_STAFF[\'depart\'][index], DATA_STAFF[\'enrolled_date\'][index]) if user_input == \'q\'.split(): break def add_func(): while True: print(\'\'\' *************************************************************** 员工录入示例:add staff_table Alex Li,25,134435344,IT,2015-10-29 *************************************************************** \'\'\') staff_infofile = open(\'staff_info.txt\',\'a+\',encoding=\'utf-8\') user_input = input("请输入您要增加员工信息的正确的语法(如果想返回上一层,请" "按q):\\n").split(\'staff_table\') if user_input == \'q\'.split(): break core_message = \',\'.join(user_input) core_message =core_message.split(\',\')[1:] STAFF_ID = int(DATA_STAFF[\'id\'][-1]) +1 DATA_STAFF[\'id\'].append(STAFF_ID) for iphone in DATA_STAFF[\'phone\']: if core_message[2] ==iphone: print("手机不允许重复,请重新添加") add_func() else: pass staff_infofile.write(\'\\n\' + str(STAFF_ID) + \',\' + \',\'.join(core_message)) staff_infofile.close() def del_func(): while True: print(\'\'\' *********************************** 删除示例:del from staff where id=3 *********************************** \'\'\') user_input= input("请输入您要删除的员工id的语法(如果想返回上一层,请按q):\\n ") if len(user_input) ==26 or len(user_input) ==27: del_staffid = user_input.split(\'=\') count = 1 del_left, del_right = del_staffid if del_right in DATA_STAFF[\'id\']: staff_index = DATA_STAFF[\'id\'].index(del_right) staff_infofile = open(\'staff_info.txt\', \'w\', encoding=\'utf_8\') print(\'\\033[31;1m员工 \' + DATA_STAFF[\'name\'][staff_index] + \' 已经删除\\033[0m\') for i in DATA_STAFF: del DATA_STAFF[i][staff_index] while True: staff_wr = DATA_STAFF[\'id\'][count] + \',\' + DATA_STAFF[\'name\'][count] + \',\'\\ + DATA_STAFF[\'age\'][count] + \',\' + DATA_STAFF[\'phone\'][count] + \',\'\\ + DATA_STAFF[\'depart\'][count] + \',\' + DATA_STAFF[\'enrolled_date\'][count] staff_infofile.write(staff_wr) count += 1 if count == len(DATA_STAFF[\'id\']): break staff_infofile.close() else: print("\\033[31;1m员工信息表中无此员工的信息,请重新输入\\033[0m") del_func() elif user_input == \'q\': break else: print("\\033[31;1m输入的语法有误,请重输!\\33[0m") del_func() def update_func(): while True: print(\'\'\' ************************************************************* 示例:UPDATE staff_table SET dept="Market" WHERE dept = "IT" UPDATE staff_table SET age=25 WHERE name = "Alex Li" ************************************************************* \'\'\') update_staff = input("请输入您要更新的员工信息的语法(如果想返回上一层,请按q):\\n ") if len(update_staff) >= 50 and len(update_staff) <= 65: user_update = update_staff.split(\'SET\') update_staff_left,update_staff_right = user_update user_update_finally = user_update[-1].strip().split(\'WHERE\') after_update = user_update_finally[0] before_update = user_update_finally[1] after_update_name,after_update_content = after_update.split(\'=\') before_update_name, before_update_content = before_update.split(\'=\') if after_update_name.strip() == before_update_name.strip(): for dept in DATA_STAFF[\'depart\']: if dept == eval(before_update_content): DATA_STAFF[\'depart\'][DATA_STAFF[\'depart\'].index(eval(before_update_content))] \\ = eval(after_update_content) count = 0 staff_infofile = open(\'staff_info.txt\', \'w\', encoding=\'utf_8\') while True: staff_wr = DATA_STAFF[\'id\'][count] + \',\' + DATA_STAFF[\'name\'][count] + \',\' \\ + DATA_STAFF[\'age\'][count] + \',\' + DATA_STAFF[\'phone\'][count] + \',\' \\ + DATA_STAFF[\'depart\'][count] + \',\' + DATA_STAFF[\'enrolled_date\'][count] staff_infofile.write(staff_wr) count += 1 if count == len(DATA_STAFF[\'id\']): break staff_infofile.close() else: for index,name in enumerate(DATA_STAFF[\'name\']): if name.strip() == eval(before_update_content).strip(): DATA_STAFF[\'age\'][index] = eval(after_update_content) else: pass count = 0 staff_infofile = open(\'staff_info.txt\', \'w\', encoding=\'utf_8\') while True: staff_wr = str(DATA_STAFF[\'id\'][count]) + \',\' + DATA_STAFF[\'name\'][count] + \',\' \\ + str(DATA_STAFF[\'age\'][count]) + \',\' + str(DATA_STAFF[\'phone\'][count]) + \',\' \\ + DATA_STAFF[\'depart\'][count] + \',\' + DATA_STAFF[\'enrolled_date\'][count] staff_infofile.write(staff_wr) count += 1 if count == len(DATA_STAFF[\'id\']): break staff_infofile.close() elif update_staff == \'q\': break else: print("\\033[31;1m输入的语法有误,请重输!\\33[0m") update_func() def main(): while True: print(prompt_func()) user_input = input("请输入要执行操作的序号>> ") user_actions = { 1: find_func, 2: add_func, 3: del_func, 4: update_func, } if user_input.isdigit(): if int(user_input) in user_actions.keys(): user_action = user_actions[int(user_input)] user_action() else: print(\'\\033[1;31m 输入错误,请重新输入 \\033[0m\') continue else: print(\'\\033[1;31m 输入错误,请重新输入 \\033[0m\') continue if __name__ == \'__main__\': main()
四,测试
4.1 主界面如下:
欢迎来到员工信息查询系统! 操作选项: 1、模糊查询员工信息 2、新增员工信息 3、删除指定员工信息 4、修改员工信息 5、quit返回上一级 请输入要执行操作的序号>>
4.2 模糊查询员工信息界面:
请输入要执行操作的序号>> 1 -------------欢迎进入模糊查询员工信息界面---------------- *************************************************** 命令行示例: find name age where age > 20 find * from staff_table where dept IT find * from staff_table where enroll_date like 2013 *************************************************** 请输入您要查询员工信息的正确的语法(如果想返回上一层,请按q):
4.3 新增员工信息界面:
请输入要执行操作的序号>> 2 -------------欢迎进入新增员工信息界面---------------- *************************************************************** 员工录入示例:add staff_table Alex Li,25,134435344,IT,2015-10-29 *************************************************************** 请输入您要增加员工信息的正确的语法(如果想返回上一层,请按q):
4.4 删除指定员工信息界面:
请输入要执行操作的序号>> 3 -------------欢迎进入删除指定员工信息界面---------------- *********************************** 删除示例:del from staff where id=3 *********************************** 请输入您要删除的员工id的语法:
4.5 修改员工信息界面:
请输入要执行操作的序号>> 4 -------------欢迎进入修改员工信息界面---------------- ************************************************************* 示例:UPDATE staff_table SET dept="Market" WHERE dept = "IT" UPDATE staff_table SET age=25 WHERE name = "Alex Li" ************************************************************* 请输入您要更新的员工信息的语法:
以上是关于项目:员工信息增删改查程序的主要内容,如果未能解决你的问题,请参考以下文章