员工信息增删改查程序
现要求写一个简单的员工信息增删改查程序,需求如下:
当然此表你在文件存储时可以这样表示
1 2 3 4 5 6 7 8 9 10 | 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种查询语法:
1 2 3 4 5 | 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需自增
1 | 语法: add staff_table Alex Li, 25 , 134435344 ,IT, 2015 - 10 - 29 |
3.可删除指定员工信息纪录,输入员工id,即可删除
1 | 语法: del from staff where id = 3 |
4.可修改员工信息,语法如下:
1 2 | 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.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。
注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!
一:题目思路图:
二,程序概述:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | * * 概述: * * 本次作业文件夹一共包含了以下 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 运行后,根据控制台显示的提示信息执行 |
三,主程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #_*_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() |
更新版的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | #_*_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 6 7 8 9 | 欢迎来到员工信息查询系统! 操作选项: 1 、模糊查询员工信息 2 、新增员工信息 3 、删除指定员工信息 4 、修改员工信息 5 、quit返回上一级 请输入要执行操作的序号>> |
4.2 模糊查询员工信息界面:
1 2 3 4 5 6 7 8 9 10 11 | 请输入要执行操作的序号>> 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 新增员工信息界面:
1 2 3 4 5 6 7 8 | 请输入要执行操作的序号>> 2 - - - - - - - - - - - - - 欢迎进入新增员工信息界面 - - - - - - - - - - - - - - - - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 员工录入示例:add staff_table Alex Li, 25 , 134435344 ,IT, 2015 - 10 - 29 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 请输入您要增加员工信息的正确的语法(如果想返回上一层,请按q): |
4.4 删除指定员工信息界面:
1 2 3 4 5 6 7 8 | 请输入要执行操作的序号>> 3 - - - - - - - - - - - - - 欢迎进入删除指定员工信息界面 - - - - - - - - - - - - - - - - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 删除示例: del from staff where id = 3 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 请输入您要删除的员工 id 的语法: |
4.5 修改员工信息界面:
1 2 3 4 5 6 7 8 9 | 请输入要执行操作的序号>> 4 - - - - - - - - - - - - - 欢迎进入修改员工信息界面 - - - - - - - - - - - - - - - - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 示例:UPDATE staff_table SET dept = "Market" WHERE dept = "IT" UPDATE staff_table SET age = 25 WHERE name = "Alex Li" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 请输入您要更新的员工信息的语法: |
不经一番彻骨寒 怎得梅花扑鼻香
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步