mysql修改员工表_员工信息表 实现增删改查操作
1 importos2 definquiry_system():3 welcome ='''
4 -----------員工信息查詢系統------------5 操作說明:6 增加ADD:17 刪除DELETE:28 修改CHANGE:39 查詢SEARCH:410 退出QUIT:511 ---------------------------------------12 '''
13 print(welcome)14
15 defadd():16 #添加語法: name,age,phone,dept,enroll-date
17 add_staff =input('請輸入要添加的員工信息:(添加格式: name,age,phone,dept(大寫),enroll-date)\n')18 list_data =add_staff.split(',')19 list_all =[]20 f = open('staff_table.txt','r+',encoding='utf8')21 for line inf:22 list_all.append(line.strip().split(',')[3])23 if list_data[2] inlist_all:24 print('用戶已存在!')25 f.close()26 else:27 for line inf:28 f.write(line)29 staff_id = str(len(list_all) + 1)30 list_data.insert(0, str(staff_id))31 f.write('\n')32 f.write(','.join(list_data))33 f.close()34 print('添加成功')35
36 defdelete():37 del_staff_id =input('請輸入要刪除的員工ID:')38 f = open('staff_table.txt','r',encoding='utf8')39 f1 = open('new_staff_table.txt','w',encoding='utf8')40 for line inf:41 each_line = line.split(',')42 if int(each_line[0]) !=int(del_staff_id):43 f1.write(line)44 #若想實現刪除后員工ID順序排列使用下三行代碼!
45 #elif int(each_line[0]) > int(del_staff_id):
46 #each_line[0] =str(int(each_line[0])-1)
47 #f1.write(','.join(each_line))
48 else:49 continue
50 f.close()51 f1.close()52 os.remove('staff_table.txt') #刪除指定目錄
53 os.rename('new_staff_table.txt', 'staff_table.txt') #重命名
54
55 defchange():56 #修改請輸入(注意空格和沒有引號):UPDATE staff_table SET dept = IT where dept = MARKET
57 data = input("請輸入您要修改的信息:(修改格式:UPDATE staff_table SET dept = IT where dept = MARKET)\n")58 old = data.split(' ')[5]59 new = data.split(' ')[9]60 f = open('staff_table.txt', 'r', encoding='utf-8')61 f1 = open('new_staff_table.txt', 'w', encoding="utf-8")62 for line inf:63 if old inline:64 line =line.replace(old, new)65 f1.write(line)66 f.close()67 f1.close()68 os.remove('staff_table.txt')69 os.rename('new_staff_table.txt', 'staff_table.txt')70 print('修改成功')71
72 defsearch():73 formation='''
74 查詢方式1:(按年齡查詢)select * from staff_table where age >= 2275 查詢方式2:(按職業查詢)select * from staff_table where dept = "IT"76 查詢方式3:(按年份查詢)select * from staff_table where enroll_date like "2013"77 '''
78 print(formation)79
80 whileTrue:81 search_way = input('請輸入您的查詢方式(退出Q):')82 res =[]83 count =084 if search_way.lower() =='q':85 exit()86 if search_way =='1':87 age_search = input('請輸入要查詢的最小年齡:')88 with open('staff_table.txt','r',encoding='utf8') as f:89 for line inf:90 each_line =line.split(',')91 if int(each_line[2]) >=int(age_search):92 res.append(line)93 count += 1
94 print("查詢的結果為:")95 for per inres:96 print(per)97 print('共有%s個員工滿足查詢信息條件!'%count)98 break
99 if search_way =='2':100 dept_search = input('請輸入要查詢的職業:')101 with open('staff_table.txt', 'r', encoding='utf8') as f:102 for line inf:103 each_line = line.split(',')104 res.append(each_line)105 result =filter(lambda l:l[4]==dept_search.upper(),res)106 for i inresult:107 count+=1
108 print(i)109 print('共有%s個員工滿足查詢信息條件!'%count)110 break
111 if search_way =='3':112 enroll_date = input('請輸入要查詢的年份:')113 with open('staff_table.txt', 'r', encoding='utf8') as f:114 for line inf:115 each_line = line.split(',')116 res.append(each_line)117 result = filter(lambda l:enroll_date in l[5],res)118 for i inresult:119 count += 1
120 print(i)121 print('共有%s個員工滿足查詢信息條件!' %count)122 break
123 else:124 print('請重新輸入!')125
126 my_choice ={127 '1':add,128 '2':delete,129 '3':change,130 '4':search,131 '5':'quit'
132 }133 whileTrue:134 inquiry_system()135 choice = input('請輸入操作序號:')136 if choice not inmy_choice:137 print("輸入錯誤,請重新輸入!")138 continue
139 if int(choice) == 5:140 exit()141 else:142 my_choice[choice]()
總結
以上是生活随笔為你收集整理的mysql修改员工表_员工信息表 实现增删改查操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OkHttp完美封装,一行搞完外部请求
- 下一篇: 有哪些值得推荐的制作统计图表的软件?小编