zoukankan      html  css  js  c++  java
  • 员工信息表

    1. 作业需求:
           当然此表你在文件存储时可以这样表示
           1,Alex Li,22,13651054608,IT,2013-04-01
           现需要对这个员工信息文件,实现增删改查操作
           
           可进行模糊查询,语法至少支持下面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"
           查到的信息,打印后,最后面还要显示查到的条数 
           可创建新员工纪录,以phone做唯一键,staff_id需自增
           可删除指定员工信息纪录,输入员工id,即可删除
           可修改员工信息,语法如下:
             UPDATE staff_table SET dept="Market" WHERE where dept = "IT"
            注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!
    2. 需求分析:
           #查询
           def select_table():
               example='''
                 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"
               '''
               line = input('请输入查询语句:%s
    '%example)
               # line = 'select * from staff_table where enroll_date like "2016"'
               res=re.search(r'select (.*) from staff_table where (staff_id|name|age|phone|dept|enroll_date) (.*) (.*)',line,re.M|re.I)
               keys = ['staff_id','name','age','phone','dept','enroll_date']
               if res:
                   print('	'+line)
                   re_1=res.group(1) #输出标签
                   re_2=res.group(2) #判断关键字
                   re_3=res.group(3) #判断符Operator
                   re_4=res.group(4).strip('"') #判断参数
                   if re_1 == "*":
                       if re_3 == "like":
                           word=re_4
                           likes = re.search(r'(d.*%s.*
    )' % word, fileinfo(), re.I)
                           if likes:
                               printf_table(likes.group().strip('
    '))
                           else:
                               print("not exsit")
                       elif re_3 == "=":
                           work = re_4
                           works = re.search(r'(d.*%s.*
    )' % work, fileinfo(), re.I)
                           if works:
                               printf_table(works.group().strip('
    '))
                           else:
                               print("not exsit")
                       else:
                           if re_2 in keys:
                               key = keys.index(re_2)
                               mesg = fileinfo().strip('
    ').split('
    ')
                               for i in mesg:
                                   if eval("%s %s %s" % (i.split(',')[key], re_3, re_4)) is True:
                                       printf_table(i)
                           else:
                               exit("%s 的参数不存在!"%re_2)
                   else:
                       if re_3 == "like":
                           data = re_1.split(',')
                           print('{0:15s}{1:15s}'.format(data[0].center(15), data[1].center(15)))
                           x = keys.index(data[0])
                           y = keys.index(data[1])
                           work = re_4
                           mesg = fileinfo().strip('
    ').split('
    ')
                           for i in mesg:
                               works = re.search(r'(d.*%s.*
    )' % work, fileinfo(), re.I)
                               if works.group().strip('
    ') in i:
                                   print("{0:15s}{1:15s}".format(i.split(',')[x].center(15), i.split(',')[y].center(15)))
                               else:
                                   pass
                       elif re_3 == "=":
                           data = re_1.split(',')
                           print('{0:15s}{1:15s}'.format(data[0].center(15), data[1].center(15)))
                           x = keys.index(data[0])
                           y = keys.index(data[1])
                           work = re_4
                           mesg = fileinfo().strip('
    ').split('
    ')
                           for i in mesg:
                               works = re.search(r'(d.*%s.*
    )' % work, fileinfo(), re.I)
                               if works.group().strip('
    ') in i:
                                   print("{0:15s}{1:15s}".format(i.split(',')[x].center(15), i.split(',')[y].center(15)))
                               else:
                                   pass
                       else:
                           data = re_1.split(',')
                           print('{0:15s}{1:15s}'.format(data[0].center(15), data[1].center(15)))
                           x = keys.index(data[0])
                           y = keys.index(data[1])
                           mesg = fileinfo().strip('
    ').split('
    ')
                           for i in mesg:
                               if eval("%s %s %s" % (i.split(',')[2], re_3, re_4)) is True:
                                   print("{0:15s}{1:15s}".format(i.split(',')[x].center(15),i.split(',')[y].center(15)))
           
               else:
                   print("输入有误")
           #添加
           def insert_m():
               #获取行数
               count=len(open("workerinfo", "rU").readlines())
               while True:
                   name=input("请输入姓名")
                   if name:
                       break
                   else:
                       print("姓名不能为空")
                       continue
               while True:
                   age=input("请输入年龄")
                   if age:
                       if 0<int(age)<150:
                           break
                       else:
                           print("超出真实年龄范围")
                           continue
                   else:
                       print("年龄不能为空")
                       continue
               while True:
                   phone=input("请输入联系电话")
                   if phone:
                       try:
                           re_phone=re.compile(r'd{11}').search(phone).group()
                       except AttributeError:
                           print("输入联系电话有误")
                           continue
                       else:
                           if re_phone == phone :
                               if search(phone):
                                   print("号码已存在")
                                   continue
                               else:
                                   break
                           else:
                               print("目前只支持大陆11位数字号码")
                               continue
                   else:
                       print("联系电话不能为空")
                       continue
               while True:
                   dept=input("请输入岗位")
                   if dept:
                       break
                   else:
                       dept="待定"
                       break
               while True:
                   enroll_date=input("请输入入职日期[格式:2009-01-01,默认为当前日期]")
                   if enroll_date:
                       try:
                           re_date=re.compile(r'^[12]d{3}-[0-9]{2}-[0-9]{2}').search(enroll_date).group()
                       except AttributeError:
                           print("输入日期格式有误")
                           continue
                       else:
                           if enroll_date == re_date and 0<int(enroll_date.split('-')[1])<13 and 0<int(enroll_date.split('-')[2])<32:
                               break
                           else:
                               print("入职日期输入有误")
                               continue
                   else:
                       enroll_date = datetime.date.fromtimestamp(time.time())
                       break
               with open("workerinfo",'a+',encoding='utf-8') as f:
                   f.write("%d,%s,%s,%s,%s,%s
    "%(count+1,name,age,phone,dept,enroll_date))
                   f.close()
           #删除
           def del_m(arg):
               while arg:
                   ms = search(arg)
                   newmsg = fileinfo().replace(ms, '')
                   writef(newmsg)
                   if arg in newmsg:
                       return "失败,%s 仍然存在于staff_table." % arg
                   else:
                       return "成功!"
               else:
                   print("你想删除的%s不在staff_table中."%arg)
                   return False
           #修改
           def update_f():
               example='UPDATE staff_table SET dept="Market" WHERE where dept = "IT"'
               while True:
                   line=input("请输入要查询语句:%s
    "%example)
                   res = re.search(r'UPDATE staff_table SET (.*) = (.*) WHERE where (.*) = (.*)', line,re.M | re.I)
                   keys = ['staff_id', 'name', 'age', 'phone', 'dept', 'enroll_date']
                   if res:
                       re_1=res.group(1)
                       re_2=res.group(2).strip('"')
                       re_3=res.group(3)
                       re_4=res.group(4).strip('"')
                       if re_1 == re_3:
                           if re_2 in keys:
                               ms1=search(re_2)
                               ms2=search(re_2).replace(re_2, re_4)
                               newmsg = fileinfo().replace(ms1, ms2)
                               writef(newmsg)
                               return ("修改成功!")
                           else:
                               print("%s 不存在!"%re_2)
                               continue
                       else:
                           print("输入有误!")
                           continue
                   else:
                       print("输入有误!")
                       continue
    3. 测试:
    
        员工信息表:
        -------------------------------------staff_table-------------------------------------
        staff_id         name           age           phone           dept       enroll_date  
           1           Alex Li           22        13651054608         IT         2013-04-01  
           2          Jack Wang          30        13304320533         HR         2015-05-03  
           3           Rain Liu          25        13835235322       Saies        2016-04-22  
           4           Make Cao          40        13561453433         HR         2009-03-01  
           5            mologa           27        13423643567        ops         2017-07-04  
        -----------------------------------------功能栏-----------------------------------------
          1.查询           2.添加           3.修改           4.删除           5.退出    
    
    请输入需要操作的功能选项:1
    
    请输入查询语句:
    
          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"
    
    select name,age from staff_table where age > 22
    
        select name,age from staff_table where age > 22
          name           age      
    
       Jack Wang          30      
    
        Rain Liu          25      
        Make Cao          40      
         mologa           27      
        -----------------------------------------功能栏-----------------------------------------
          1.查询           2.添加           3.修改           4.删除           5.退出     
    
    请输入需要操作的功能选项:1
    
    请输入查询语句:
    
          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"
    
    select name,age from staff_table where age = 30
    
        select name,age from staff_table where age = 30
          name           age      
    
       Jack Wang          30      
    
        -----------------------------------------功能栏-----------------------------------------
          1.查询           2.添加           3.修改           4.删除           5.退出     
    
    请输入需要操作的功能选项:1
    
    请输入查询语句:
    
          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"
    
    select * from staff_table where age > 22
    
        select * from staff_table where age > 22
           2          Jack Wang          30        13304320533         HR         2015-05-03  
           3           Rain Liu          25        13835235322       Saies        2016-04-22  
           4           Make Cao          40        13561453433         HR         2009-03-01  
           5            mologa           27        13423643567        ops         2017-07-04  
        -----------------------------------------功能栏-----------------------------------------
          1.查询           2.添加           3.修改           4.删除           5.退出     
    
    请输入需要操作的功能选项:5
    
    退出员工信息!
    

      

  • 相关阅读:
    IOS调试下载的demo出现说项目不能在什么的SDK调试
    IOS手势基本用法
    IOS没有服务器断怎么调试Push代码
    VS Tips (Advance part)
    [转]如何理解C runtime library (C运行时库)
    Use AQTime to find the bottleneck of program module
    [转]Reflection: Discovery and Execution
    如何禁止生成stack对象或heap对象
    VS Tips (Basic part)
    栈对象、堆对象、静态对象的比较
  • 原文地址:https://www.cnblogs.com/mologa-jie/p/7368073.html
Copyright © 2011-2022 走看看