python实战演练(六)员工信息查询系统

一 实现功能

(1).工信息表程序,实现增删改查操作:

(2).可进行模糊查询,语法至少支持下面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"
(3).查到的信息,打印后,最后面还要显示查到的条数

(4).可创建新员工纪录,以phone做唯一键,staff_id需自增

(5).可删除指定员工信息纪录,输入员工id,即可删除

(6).可修改员工信息,语法如下:
  UPDATE staff_table SET dept = "Market" where dept = "IT"

二 流程图

三 代码

  1.目录结构  

 1 |——员工信息查询系统
 2 |——bin目录
 3 |    |—— _init.py     
 4 |    |____    Stary.py      ——>程序运行文件
 5 |
 6 |——core目录
 7 |    |——    __init__.py
 8 |    |——    main.py    ——>主逻辑函数模块
 9 |    |——    parses.py   ——>语句解析模块
10 |    |____   action.py    ——>语句执行模块
11 |
12 |——db目录
13 |    |——    emp    ——>数据库txt文件
14 |    |___    xmp     ——>数据库txt文件
15 |
16 |__  __init.py__
目录结构

  2.core目录

  1 #-*- Coding:utf-8 -*-
  2 # Author: kking
  3 import os,sys
  4 # BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  5 # sys.path.append(BASE_DIR)
  6 # print("PATH",sys.path)
  7 
  8 from core import parses
  9 
 10 def main_parse(user_input):
 11     '''
 12     定义一个main_parse函数,来接受用户操作菜单的选择,并根据用户输入的操作序号进入相应的模块
 13     :param user_input:用户输入操作菜单序号
 14     :return:
 15     '''
 16     main_dict = {
 17         '1': main_select,
 18         '2': main_add,
 19         '3': main_update,
 20         '4': main_delect,
 21     }
 22     if user_input in main_dict.keys():
 23         main_dict[user_input]()  # 执行输入号码对应的函数
 24                                 #main_select等主函数没有定义形参所以main_dict[user_input]()括号里不要传参数
 25     if user_input == '5':
 26         exit("已退出程序,欢迎下次使用")
 27     else:
 28         print("\033[31;1m输入格式无效\033[0m")
 29 
 30 def main_select():
 31     '''
 32     定义main_select函数——select查询信息管理模块
 33     用来接受解析并完成的select语句,并显示查询结果
 34     :return:
 35     '''
 36     print('''\t\t\t-----------------------------------------------------------------------------------            
 37                 语法示例:
 38                 select name,age from db.emp where age > 22
 39                 select * from db.xmp where dept like IT
 40                 select * from db.emp where id >= 2
 41                 select * from db.emp where id <5 limit 3
 42 \t\t\t-----------------------------------------------------------------------------------''')
 43     while True:
 44         user_sql = input('请输入查询sql语句>>>:').strip()
 45         sql_list = user_sql.split(' ')  # 将用户输入的sql语句转换成列表格式
 46         func = sql_list[0]
 47 
 48         if func != 'select':
 49             print('\033[31;1m请输入相应sql关键字\033[0m')
 50             if user_sql == 'b':
 51                 break
 52         else:
 53             parses.parse(user_sql,func,sql_list)
 54 
 55 def main_add():
 56     '''
 57     定义main_add函数——insert查询信息管理模块
 58     用来接受解析并完成的insert语句,并显示查询结果
 59     :return:
 60     '''
 61     print('''\t\t\t-----------------------------------------------------------------------------------            
 62                 语法示例:
 63                 insert db.emp value Mark,32,13655818285,CTO,2014-08-08
 64                 insert db.xmp value Mark,32,13655818285,CTO,2014-08-08
 65 \t\t\t-----------------------------------------------------------------------------------''')
 66     while True:
 67         user_sql = input('请输入查询sql语句>>>:').strip()
 68         sql_list = user_sql.split(' ')  # 将用户输入的sql语句转换成列表格式
 69                                         # split()输出结果为[]此时会报错  建议split(' ')输出结果['']
 70                                         # 以空格为分隔符切分成列表形式
 71         func = sql_list[0]
 72         if func != 'insert':
 73             print('\033[31;1m请输入相应sql关键字\033[0m')
 74             if user_sql == 'b':
 75                 break
 76         else:
 77             parses.parse(user_sql,func,sql_list)
 78 
 79 
 80 def main_update():
 81     '''
 82     定义main_update函数——update查询信息管理模块
 83     用来接受解析并完成的update语句,并显示查询结果
 84     :return:
 85     '''
 86     print('''\t\t\t-----------------------------------------------------------------------------------            
 87                 语法示例:
 88                 update db.xmp set dept = Market where dept like IT
 89                 update db.emp set phone = 15618285621 where phone = 110
 90                 update db.emp set enroll_data = 2014-08-11 where dept like 运维
 91 \t\t\t-----------------------------------------------------------------------------------''')
 92     while True:
 93         user_sql = input('请输入查询sql语句>>>:').strip()
 94         sql_list = user_sql.split(' ')  # 将用户输入的sql语句转换成列表格式
 95         func = sql_list[0]
 96         if func != 'update':
 97             print('\033[31;1m请输入相应sql关键字\033[0m')
 98             if user_sql == 'b':
 99                 break
100         else:
101             parses.parse(user_sql,func,sql_list)
102 
103 
104 def main_delect():
105     '''
106     定义main_delect函数——delect查询信息管理模块
107     用来接受解析并完成的delect语句,并显示查询结果
108     :return:
109     '''
110     print('''\t\t\t-----------------------------------------------------------------------------------            
111                 语法示例:
112                 delect from db.emp
113                 delect from db.emp where id = 3 
114                 delect from db.emp where  id < 10 and name like alex
115 \t\t\t-----------------------------------------------------------------------------------''')
116     while True:
117         user_sql = input('请输入查询sql语句>>>:').strip()
118         sql_list = user_sql.split(' ')  # 将用户输入的sql语句转换成列表格式
119         func = sql_list[0]
120 
121         if func != 'delect':
122             print('\033[31;1m请输入相应sql关键字\033[0m')
123             if user_sql == 'b':
124                 break
125         else:
126             parses.parse(user_sql,func,sql_list)
main主逻辑板块
  1 #-*- Coding:utf-8 -*-
  2 # Author: kking
  3 import os,sys
  4 from core import action
  5 def parse(user_sql,func,sql_list):
  6     '''
  7     定义用户输入的sql并统一格式化后分配到各个sql解析模块
  8     :param user_sql:用户输入的sql语句
  9     :return:
 10     '''
 11     parse_dic = {
 12         'select':select_parse,
 13         'update':update_parse,
 14         'delect':delect_parse,
 15         'insert':insert_parse
 16     }
 17     par_res = ''
 18     if func in parse_dic.keys():    #根据用户输入的sql关键字,分配到相应函数中进行解析
 19         par_res = parse_dic[func](sql_list) #将parse_dic[func](sql_list)中的(sql_list)作为位置参数传给select_parse()
 20                         #select_parse()有定义个形参,所以parse_dic[func]()后要定义一个位置参数给select_parse()
 21     return par_res                      #定义一个函数返回值,传给相应的解析函数
 22 
 23 def select_parse(par_res):
 24     '''
 25     定义select_parse函数,接受用户输入的查询sql语句(parse()函数传递过来的返回值 res=parse_dic[func](sql_list))
 26     并返回参数给hand_parse(res,sql_dic)函数进行sql解析
 27     :param sql:
 28     :return:
 29     '''
 30     sql_dic = {
 31         'par_res': action.select_action,
 32         'select':[],
 33         'from':[],
 34         'where':[],
 35         'limit':[]
 36     }
 37     #print('in the select_parse-parse_res:',par_res,sql_dic)
 38     return hand_parse(par_res,sql_dic)
 39 
 40 
 41 def insert_parse(par_res):
 42     '''
 43     定义insert_parse函数,接受用户输入的查询sql语句并进行sql解析
 44     :param sql:
 45     :return:
 46     '''
 47     sql_dic = {
 48         'par_res': action.insert_action,
 49         'insert': [],
 50         'value': [],
 51         'into':[]
 52     }
 53     print('in the insert_parse:', par_res)
 54     return hand_parse(par_res, sql_dic)
 55 
 56 
 57 def update_parse(par_res):
 58     '''
 59     定义update_parse函数,接受用户输入的查询sql语句并进行sql解析
 60     :param sql:
 61     :return:
 62     '''
 63     sql_dic = {
 64         'par_res': action.update_action,
 65         'update': [],
 66         'set': [],
 67         'where': [],
 68     }
 69     #print('in the update_parse:',par_res)
 70     return hand_parse(par_res, sql_dic)
 71 
 72 def delect_parse(par_res):
 73     '''
 74     定义delect_parse函数,接受用户输入的查询sql语句并进行sql解析
 75     :param sql:
 76     :return:
 77     '''
 78     sql_dic = {
 79         'par_res':action.delect_action,
 80         'delect': [],
 81         'from': [],
 82         'where':[]
 83     }
 84     #print('in the delect_parse:',par_res)
 85     return hand_parse(par_res, sql_dic)
 86 
 87 def hand_parse(par_res,sql_dic):
 88     '''
 89     该函数把接受过来的函数进行格式化解析操作,并将整合后的sql字典作为返回值传参给  sql_action()语句主执行函数
 90     :param sql_list:
 91     :param sql_dic:各sql模块所对应的sql语句结构字典
 92     :return:
 93     '''
 94     for item in par_res:        #循环遍历select_parse()函数传过来的:用户输入的sql语句
 95         if item in sql_dic:     #判断语句中的关键字是否在相应函数sql_dic字典中
 96             key = item
 97         else:
 98             sql_dic[key].append(item)   #将字典转化为 select:[*]  from:[db.emp] 格式
 99     #print('in the hand_parse:',sql_dic)
100 
101     if sql_dic.get('where'):        #整理并格式化where[id < 10...]字段内容
102         res_list = []
103         key = ['and','or','not']
104         char = ''
105         for item in sql_dic.get('where'):
106             if len(item) == 0:
107                 continue
108             if item in key:
109                 if len(char) != 0:
110                     char = where_parse(char)  #将char最为实参传参给where_parse()函数。例:char = 'id ','>','10'
111                     res_list.append(char)
112                     res_list.append(item)
113                     char = ''
114             else:
115                 char += item
116         else:
117             char = where_parse(char)    ##将char最为实参传参给where_parse()函数。例:char = 'id ','>','10'
118             res_list.append(char),
119         sql_dic['where'] = res_list         #将where列表内容整理成 where['id > 10','and','id < 20']的格式
120         #print('where字段列表元素:',sql_dic['where'],sql_dic)
121     return  action.sql_action(sql_dic)     #将where['id > 10','and','id < 20']的列表格式作为返回值,
122                                             # 传参给where_parse()函数进行最终整理
123 
124 def where_parse(where_char):
125     '''
126     该函数接收hand_parse()传递过来的char参数,并把这些参数整理成['id ','>','10']这样的格式,并返回一个where_res_list值
127     :param where_char: where_parse(where_char)函数中where_char形参接收的是hand_parse()传递过来的char参数
128     :return: 把整理完毕的参数格式作为一个 where_res_list列表 的返回值
129     '''
130     key = ['<','>','=']
131     where_res_list = []
132     opt = ''
133     char = ''
134     tag = False
135     for item in where_char: #循环遍历where_char字符串,如: 'id > 10'
136         if len(item) == 0:
137             continue
138         if item in key:
139             tag = True      #将tag状态变为True
140             if len(char) != 0 :
141                 where_res_list.append(char)
142                 char = ''
143             opt += item
144         if not tag:         #判断tag状态是否是False
145             char += item
146         if tag and item not in key:     #判断tag状态为False并且不在key列表中
147             tag = False                 #将tag状态变为False
148             where_res_list.append(opt)
149             opt = ''
150             char += item
151     else:
152         where_res_list.append(char)
153 
154     if len(where_res_list) == 1 : # 将列表中'namelikealex'字符串内容转换成['name','like','alex']格式
155         where_res_list = where_res_list[0].split('like')
156         where_res_list.insert(1,'like')
157     #print('in the where_parse:', where_res_list)
158     return where_res_list
prase语句解析板块
  1 #-*- Coding:utf-8 -*-
  2 # Author: kking
  3 import os,sys,re
  4 
  5 #语句主分配模块
  6 def sql_action(sql_dic):
  7     '''
  8     该函数接收hand_parse()传过来的整理完毕后的字典sql_dic,并根据字典中 par_res键分配调用相应的语句执行模块函数
  9     :return:
 10     '''
 11     return sql_dic.get('par_res')(sql_dic)  #根据字典中 par_res为键 将sql_dic做为参数分配调用相应的语句执行模块函数
 12 
 13 #select查询语句执行模块
 14 def select_action(sql_dic):
 15     '''
 16     该函数通过sql_action主语句执行函数传来的参数‘sql_dic字典’进行语句执行操作
 17     :param sql_dic: sql_action主语句执行函数传来的参数
 18     :return:
 19     '''
 20     #优先处理最高from部分
 21     if len(sql_dic.get('from')) == 0:
 22         print('\033[31;1mfrom字段不能为空\033[0m')
 23     else:
 24         db,table = sql_dic.get('from')[0].split('.') #将{from:['db.emp'}中['db.emp']拆分成 table = emp  db = db
 25         db_pash = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + r'/%s/%s'%(db,table)
 26         with open(db_pash,'r',encoding='utf-8')as fh:
 27 
 28     #其次处理where部分
 29             where_res = where_action(fh,sql_dic.get('where'))  #定义where_res变量存储where_action的执行处理结果
 30     # 再次处理limit部分
 31             limit_res = limit_action(where_res,sql_dic.get('limit'))
 32     # 最后处理select部分
 33             select_res = select(limit_res,sql_dic.get('select'))
 34             for record in select_res:
 35                 print("查询结果为: \033[34;1m%s\033[0m"% record)
 36             print('查询结果数量为: \033[34;1m%s\033[0m'%len(record))
 37             return select_res
 38 
 39 def where_action(fh, where_l):
 40     '''
 41     该函数将接收过来的db.emp文件变成字典形式,并将该字典与用户输入的where条件进行对比解析,最后将对比结果为True的查询
 42     条件储存在where_action_res列表中
 43     :param fh: 接收select_action函数传过来的db.emp文件路径
 44     :param where_l:接收 select_action函数传过来的sql_dic.get(where')
 45     :return:
 46     '''
 47     where_action_res = []
 48     title = 'id,name,age,phone,dept,enroll_data'
 49     if len(where_l) != 0:
 50         for item in fh:
 51             db_dic = dict(zip(title.split(','), item.split(',')))
 52             '''
 53             定义db_dic函数以字典形式将emp文件中的内容为值,title
 54             为字典中的键做下拉链拼接。例:db_dic = {
 55                                                 'name':'Mark'
 56                                                 'age':'4'
 57                                                     ......
 58                                                     }
 59             '''
 60             logice_res = logic_action(where_l,db_dic)  # logice_res  = logic_action_res
 61             if logice_res:  # 判断logic_action的返回结果是否为True
 62                 where_action_res.append(item.split())  # 将fh和where_l比对后都为True的那条记录添加到where_action_res列表
 63             else:           # 查询结果都为False,给出提示
 64                 print('正在努力为您查询请稍后...')
 65     else:
 66         where_action_res = fh.readlines()
 67     #print('in the where_action_res: \033[32;1m%s\033[0m'%where_action_res)
 68     return where_action_res
 69 
 70 def logic_action(where_l,db_dic):
 71     '''
 72     该函数处理where部分与db.emp文件中的信息进行逻辑分析和对比。并将对比结果为True的信息返回给where_action
 73     :param where_l:
 74     :param db_dic:
 75     :return:
 76     '''
 77     logic_action_res = []
 78     for exp in where_l:
 79         if type(exp) is list:  # 判断exp是否是列表形式 ['id','>','10']
 80             exp_k, opt, exp_v = exp  # 将该列表参数赋值成 exp_k=='id'  opt = '>' ,exp_v = '10'
 81             if exp[1] == '=':
 82                 opt = "%s=" % exp[1]  # 将exp列表中 '=' 变为 '=='
 83             if db_dic[exp_k].isdigit():  # 判断 db_dic['id'] 是否是数字  如:10
 84                 dic_v = int(db_dic[exp_k])  # 将  db_dic['id']的值变成int类型
 85                 exp_v = int(exp_v)
 86             else:
 87                 dic_v = '%s' % db_dic[exp_k]  # 将不是数字的例如: 员工姓名 alex,Jim
 88             if opt != 'like':
 89                 exp = str(eval('%s%s%s' % (dic_v, opt, exp_v)))  # 将emp表中员工db_dic[exp_k]值与exp_v值进行eval字符串比较
 90             else:
 91                 if exp_v in dic_v:
 92                     exp = 'True'
 93                 else:
 94                     exp = 'False'
 95         logic_action_res.append(exp)  # 将exp  "'True',and,'False'" 字符串变成['True',and,'False']形式
 96     logic_action_res = eval(' '.join(logic_action_res))  # 先将['True',and,'False']使用join函数变成'Falase',然后在用
 97     # eval函数最终将logic_action_res变成False
 98     #print('in the logic_action_res\033[33;1m%s\033[0m' %logic_action_res)
 99     return logic_action_res
100 
101 def limit_action(where_res,limit_l):
102     limit_res = []
103     if len(limit_l) != 0:
104         index = int(limit_l[0])         #定义index变量取 limit_l[0]所对应的值 如 limit['3'] index=3
105         limit_res = where_res[0:index]  #将where_res里面的内容进行切片,从0-index
106     else:
107         limit_res = where_res
108     return limit_res
109 
110 def select(limit_res,select_l):
111     '''
112     该函数为执行select[name,id]模块查询语句,也是其最终查询结果。如用户输入 select * from db.emp则显示所有字段结果
113     若 select name,id,dept from db.emp 则只显示 name,age,dept字段的查询结果
114     :param limit_res: limit_res函数过滤后的查询结果
115     :param select_l: 用户输入的 select ['name','age','dept']列表信息
116     :return:
117     '''
118     select_list = []
119     exp = []
120     char = ''
121     if len(select_l) != 0:
122         if select_l[0] != '*':
123             title = 'id,name,age,phone,dept,enroll_data'
124             for item in limit_res:
125 
126                 for index in item:
127                     select_dic = dict(zip(title.split(','),index.split(',')))
128                     exp = select_l[0].split(',')
129                     for i in exp:
130                         select_list.append(select_dic[i].strip())
131         else:
132             select_list = limit_res
133     else:
134         print('\033[31;1m请输入有效select * 语句\033[0m')
135     return exp,select_list
136 
137 #insert语句执行模块
138 def insert_action(sql_dic):
139     '''
140     该函数接收用户输入的insert语句,并分配给指定的insert执行函数进行原文件对比和执行程序
141     :param sql_dic:
142     :return:
143     '''
144     #首先处理insert部分
145     if len(sql_dic.get('insert')) == 0:
146         print('\033[31;1m insert 字段不能为空\033[0m')
147     else:
148         db,table = sql_dic.get('insert')[0].split('.') #将{from:['db.emp'}中['db.emp']拆分成 table = emp  db = db
149         db_pash = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + r'/%s/%s'%(db,table)
150         with open(db_pash,'r',encoding='utf-8')as fh:
151     #其次处理value值
152             value_res = value_action(fh,sql_dic.get('value'),db_pash)
153     return value_res
154 
155 def value_action(fh,value_l,db_pash):
156     '''
157     该函数接收insert_action()函数传递过来的fh,value_l,db_pash值,并相应变量进行解析整理并执行用户输入的insert语句
158     :param fh: 数据库文件
159     :param value_l: 用户输入的 value字段参数
160     :param db_pash: 数据库文件路径
161     :return:
162     '''
163     phone = []
164     for index in value_l:
165         index_l = index.split(',')  #遍历用户输入的value值,并将其转换为['5','Mark','32'....]格式
166     for item in fh:               #遍历数据库表文件也将其转换为['5','Mark','32'....]格式
167         info = item.strip().split(',')
168         phone.append(info[3])
169 
170     tag = True
171     if index_l[2] in phone:   #以手机号作唯一键,判断用户输入的value值是否存在数据文件中
172         tag = False
173         tag_res = print('\033[31;1m该用户已存在不能重复添加\033[0m')
174     if tag and len(index_l) < 5:
175         tag = False
176         tag_res = print('\033[31;1m用户输入value信息不够\033[0m')
177     if tag:
178         index_l[0] = int(info[0][-1]) + 1  # 完成id自增:info[0][-1] 取info列表 id的字段最后一个值,然后自动+1
179         with open(db_pash,'a',encoding='utf-8') as f:
180             f.write(''.join('\n%s,'%index_l[0]+index)) #使用join函数将['6','mark','32'...]拼接字符串成 8,Mark,32的样式
181             tag_res = print("已成功添加信息: \033[34;1m%s\033[0m" %index)
182     return tag_res,index_l
183 
184 #update语句执行模块
185 def update_action(sql_dic):
186     #优先处理update字段
187     db,table = sql_dic.get('update')[0].split('.') #将{from:['db.emp'}中['db.emp']拆分成 table = emp  db = db
188     db_pash = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + r'/%s/%s'%(db,table)
189     with open(db_pash,'r',encoding='utf-8') as fh:
190 
191     #其次处理where部分
192         where_res = where_action(fh,sql_dic.get('where'))  # 定义where_res变量存储where_action的执行处理结果
193     #最后处理set部分
194     set_res = set_action(where_res,sql_dic.get('set'),fh,db_pash)
195     print('参数已修改完成: \033[36;1m %s \033[0m' %set_res)
196     return set_res
197 
198 def set_action(where_res,set_l,fh,db_pash):
199     '''
200     该函数对用户输入的set字段进行处理执行,返回添加结果和修改数据库文本内容
201     :param where_res: 接收where_action()函数传递过来的where_res返回值
202     :param set_l: 用户输入的 set列表参数
203     :param fh:
204     :param db_pash:
205     :return:
206     '''
207     set_list = []
208     change_list = []
209     title = 'id,name,age,phone,dept,enroll_data'
210     if len(set_l) == 0 or set_l[0] == 'id':
211         print('\033[31;1m用户id不能修改\033[0m')
212     else:
213         for item in where_res:
214             for index in item:   # index参数格式: 1,'Alex',22...
215                 index_l= index.split(',')   #index_l参数格式:['1','Alex','22'...]
216             set_dic = dict(zip(title.split(','),index_l))
217 
218             change_list.append(set_dic[set_l[0]])  # 将未修改的字段参数值添加到change_list列表
219             change_list.append(set_l[2])  # 将需要修改的参数添加到change_list列表
220 
221             set_dic[set_l[0]] = set_l[2]  # 将字典根据用户输入的要修改的字段 如: dept,age 修改成 相应的数值
222 
223             set_list = (list(set_dic.values()))    #将重新赋值后的值取出并以列表形式存储,作为修改后的列表
224             with open(db_pash, 'r', encoding='utf-8')as fh:
225                 fh_r = fh.readlines()
226 
227             with open(db_pash,'w',encoding='utf-8') as f:
228                 for line in fh_r:
229                     if change_list[0] in line :   #判断未修改的参数值是否存在数据库表中
230                             line = line.replace(change_list[0],change_list[1])  #修改文件中所对应的参数值
231                     f.write(line)
232 
233     #print('in the set_action: \033[36;1m %s \033[0m'%set_list)
234     return set_list
235 
236 #delect语句执行模块
237 def delect_action(sql_dic):
238     '''
239     delect主执行函数,对用户输入的delect语句各字段进行分配到相应函数进行解析执行
240     :param sql_dic:
241     :return:
242     '''
243     # 优先处理from字段
244     if len(sql_dic.get('from')) == 0:
245         print('\033[31;1m insert 字段不能为空\033[0m')
246     else:
247         db, table = sql_dic.get('from')[0].split('.')  # 将{from:['db.emp'}中['db.emp']拆分成 table = emp  db = db
248         db_pash = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + r'/%s/%s' % (db, table)
249         with open(db_pash, 'r', encoding='utf-8')as fh:
250     #其次处理where字段
251             where_res = where_action(fh,sql_dic.get('where'))  # 定义where_res变量存储where_action的执行处理结果
252     #最后处理delect字段
253     delect_res = delect(fh,where_res,where_l=sql_dic.get('where'),db_pash=db_pash)
254     print('已删除\033[34;1m %s \033[0m员工信息:'%delect_res)
255     return delect_res
256 
257 def delect(fh,where_res,where_l,db_pash):
258     '''
259     该函数执行用户输入的 where条件参数解析并执行delect操作,并相应文本内容
260     :param fh:
261     :param where_res:
262     :param where_l:
263     :param db_pash:
264     :return:
265     '''
266     delect_list = []
267     for i in where_res:
268         for i in i:
269             pass
270         if len(where_l) != 0:
271             with open(db_pash,'r',encoding='utf-8') as fh:
272                 lines = fh.readlines()
273                 for line in lines:
274                     if not re.search(i,line): #循环遍历出 不包含 想要删除的文本信息
275                         delect_list.append(line)    #并将这些信息存储到字典中
276             with open(db_pash,'w',encoding='utf-8') as f:
277                 f.writelines(delect_list)           #将不包含想要删除的文本信息 写入数据库文本中
278         else:
279             print('\033[31;1m无删除条件记录\033[0m')
280     return where_res
action语句执行板块

  3.db数据目录结构

1 1,zhiww,29,18898564892,Market,2013-02-12
2 2,wangying,12,100861343251,Market,2014-08-11
3 3,zhoujielun,29,15618285621,运维,2014-08-11
4 4,zhangsan,36,100861221,HR,2014-08-11
5 5,zhoujielun,29,114550,测试,2007-02-12
6 6,alex,25,13798708765,测试,2015-06-15
7 7,chen,29,1889856413,Market,2013-02-12
8 8,zjlun,25,13569856669,HR,2014-08-11
9 9,Mark,32,13655818285,CTO,2014-08-08
db

 

猜你喜欢

转载自www.cnblogs.com/kking-lh/p/10107694.html
今日推荐