python作业---员工信息的增删改查

作业需求


1.可进行模糊查询,语法至少支持下面3种:
   select name,age from staff_table where age > 22
   select * from staff_table where dept = "IT"
select * from staff_table where enroll_date like "2013"
查到的信息,打印后,最后面还要显示查到的条数
2.可创建新员工纪录,以phone做唯一键,staff_id需自增
3.可删除指定员工信息纪录,输入员工id,即可删除
4.可修改员工信息,语法如下:
   UPDATE staff_table SET dept="Market" WHERE where dept = "IT"


作业思路
1.把需要查询的内容转成列表形式
2.逐行读取文件内容,存成列表形式
3.根据读取的内容与需求进行适配
4.符合要求的进行打印或者重新写入文件

流程图

 

xinxi:

1,Alex,22,13651054608,运维,2013-04-01
2,Jack,23,13655818285,Web,2013-04-06
3,Mack,20,13651854708,HR,2013-05-06
4,Gao,22,18651054608,运维,2012-09-01
5,Rain,32,15125154608,CTO,2000-01-01

 

  1 import os
  2 # select name,age from staff_table where age > 22
  3 # select  * from staff_table where dept = "IT"
  4 # select  * from staff_table where enroll_date like "2013"
  5 def query(data):
  6     data1 = data.split(" ")
  7     if data ==("select name,age from staff_table where age > %s"%(data1[7])):
  8         with open("xinxi","r", encoding="utf-8") as f:
  9             list = []
 10             for line in f:
 11                 i = line.strip().split(",")
 12                 if i[2] > data1[7]:
 13                     list.append(i)
 14                     # print(i)
 15             for k in list:
 16                 print(k)
 17             print("查询到 %s 条符合的信息" % len(list))
 18     elif data == ("select * from staff_table where dept = %s" % (data1[7])):
 19             with open("xinxi","r", encoding="utf-8") as f:
 20                 list1 = []
 21                 for line in f:
 22                     i1 = line.strip().split(",")
 23                     if i1[4] == data1[7]:
 24                         list1.append(i1)
 25                 for j in list1:
 26                     print(j)
 27                 print("查询到 %s 条符合的信息" % len(list1))
 28     else:
 29         if data == ("select * from staff_table where enroll_date like %s" % (data1[7])):
 30             with open("xinxi","r", encoding="utf-8") as f:
 31                 list = []
 32                 for line in f:
 33                     i = line.strip().split(",")
 34                     if i[5][0:4]==data1[7]:
 35                         list.append(i)
 36                 for k in list:
 37                     print(k)
 38                 print("查询到 %s 条符合的信息" % len(list))
 39     return 0
 40 
 41 #name,age,phone,dept,enroll-date
 42 def add(data):
 43     data1=data.split(",")
 44     with open ("xinxi","r",encoding="utf-8") as f:
 45         list=[]
 46         phone_list=[]
 47         for line in f:
 48             i=line.strip().split(",")
 49             phone_list.append(i[3])
 50         if data1[2] in phone_list:
 51             print("手机号码已存在!")
 52         else:
 53             with open ("xinxi","r+",encoding="utf-8") as f1:
 54                 for line in f1:
 55                     i1=line.strip().split(",")
 56                     list.append(i1)
 57                 w=str(int(list[-1][0])+1)
 58                 data1.insert(0,w)
 59                 print(data1)
 60                 data1=",".join(data1)
 61                 f1.write("\n")
 62                 f1.write(data1)
 63                 print("添加成功!")
 64     return 0
 65 
 66 #delete from staff_table where staff_id = 12
 67 def delete(data):
 68     data1=data.split(" ")
 69     if data==("delete from staff_table where staff_id = %s"%(data1[6])):
 70         with open("xinxi","r",encoding="utf-8") as f:
 71             list=[]
 72             for line in f:
 73                 i =line.strip().split(",")
 74                 i1=line.splitlines()
 75                 if data1[6]==i[0]:
 76                     i2=",".join(i1)
 77                     print(i2)
 78                     list.append(i)
 79         a=i2
 80         f=open ("xinxi","r",encoding="utf-8")
 81         f1=open ("xinxi_new","a+",encoding="utf-8")
 82         for i in f:
 83             if a in i:
 84                 i=i.replace(a,"").strip()
 85             f1.write(i)
 86             f1.flush()
 87         f.close()
 88         f1.close()
 89     os.remove("xinxi")
 90     os.rename("xinxi_new","xinxi")
 91     print("删除成功")
 92     return 0
 93 
 94 #UPDATE staff_table SET dept = IT where dept = 运维
 95 def change(data):
 96     data1=data.split(" ")
 97     with open("xinxi","r",encoding="utf-8") as f,open("xinxi2","w",encoding="utf-8") as f1:
 98         for line in f:
 99             i = line.strip()
100             print(i)
101             if data1[5] in i:
102                 i = i.replace(data1[5],data1[9])
103             f1.write(i)
104             f1.write("\n")
105             f1.flush()
106     os.remove("xinxi")
107     os.rename("xinxi2","xinxi")
108     print("修改成功")
109     return 0
110 
111 
112 #主程序
113 while True:
114     print('''
115             ============================
116                 欢迎来到员工信息系统
117             ----------------------------
118                     1.查询
119                     2.添加
120                     3.修改
121                     4.删除
122                     5.退出
123             ===========================
124     ''')
125     choice=input("请输入序号:")
126     if choice=="1":
127         print("\t\t\t=====================语法实例=====================")
128         print("""
129             select name,age from staff_table where age > 22
130             select * from staff_table where dept = IT
131             select * from staff_table where enroll_date like 2013
132         """)
133         data = input("请输入语法>>:").strip()
134         query(data)
135 
136     if choice =="2":
137         print("\t\t\t=====================语法实例=====================")
138         print("""
139               name,age,phone,dept,enroll-date
140               """)
141         data = input("请输入语法>>:").strip()
142         add(data)
143 
144     if choice=="3":
145         print("\t\t\t=====================语法实例=====================")
146         print("""
147             UPDATE staff_table SET dept = IT where dept = 运维
148             """)
149         data = input("请输入语法>>:").strip()
150         change(data)
151 
152     if choice=="4":
153         print("\t\t\t=====================语法实例=====================")
154         print("""
155             delete from staff_table where staff_id = 12
156             """)
157         data = input("请输入语法>>:").strip()
158         delete(data)
159 
160     if choice=="5":
161         break
View Code

 


posted @ 2017-05-16 14:51  Garrett0220  阅读(791)  评论(0编辑  收藏  举报
levels of contents