1、作业需求:
(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"

注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码

2、流程图

3、目录结构

  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__

4、core目录

main主逻辑模块

  1. #-*- Coding:utf-8 -*-
  2. # Author: D.Gray
  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. '': main_select,
  18. '': main_add,
  19. '': main_update,
  20. '': 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 == '':
  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. def main_update():
  80. '''
  81. 定义main_update函数——update查询信息管理模块
  82. 用来接受解析并完成的update语句,并显示查询结果
  83. :return:
  84. '''
  85. print('''\t\t\t-----------------------------------------------------------------------------------
  86. 语法示例:
  87. update db.xmp set dept = Market where dept like IT
  88. update db.emp set phone = 15618285621 where phone = 110
  89. update db.emp set enroll_data = 2014-08-11 where dept like 运维
  90. \t\t\t-----------------------------------------------------------------------------------''')
  91. while True:
  92. user_sql = input('请输入查询sql语句>>>:').strip()
  93. sql_list = user_sql.split(' ') # 将用户输入的sql语句转换成列表格式
  94. func = sql_list[0]
  95. if func != 'update':
  96. print('\033[31;1m请输入相应sql关键字\033[0m')
  97. if user_sql == 'b':
  98. break
  99. else:
  100. parses.parse(user_sql,func,sql_list)
  101.  
  102. def main_delect():
  103. '''
  104. 定义main_delect函数——delect查询信息管理模块
  105. 用来接受解析并完成的delect语句,并显示查询结果
  106. :return:
  107. '''
  108. print('''\t\t\t-----------------------------------------------------------------------------------
  109. 语法示例:
  110. delect from db.emp
  111. delect from db.emp where id = 3
  112. delect from db.emp where id < 10 and name like alex
  113. \t\t\t-----------------------------------------------------------------------------------''')
  114. while True:
  115. user_sql = input('请输入查询sql语句>>>:').strip()
  116. sql_list = user_sql.split(' ') # 将用户输入的sql语句转换成列表格式
  117. func = sql_list[0]
  118.  
  119. if func != 'delect':
  120. print('\033[31;1m请输入相应sql关键字\033[0m')
  121. if user_sql == 'b':
  122. break
  123. else:
  124. parses.parse(user_sql,func,sql_list)

parse语句解析模块

  1. #-*- Coding:utf-8 -*-
  2. # Author: D.Gray
  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. def insert_parse(par_res):
  41. '''
  42. 定义insert_parse函数,接受用户输入的查询sql语句并进行sql解析
  43. :param sql:
  44. :return:
  45. '''
  46. sql_dic = {
  47. 'par_res': action.insert_action,
  48. 'insert': [],
  49. 'value': [],
  50. 'into':[]
  51. }
  52. print('in the insert_parse:', par_res)
  53. return hand_parse(par_res, sql_dic)
  54.  
  55. def update_parse(par_res):
  56. '''
  57. 定义update_parse函数,接受用户输入的查询sql语句并进行sql解析
  58. :param sql:
  59. :return:
  60. '''
  61. sql_dic = {
  62. 'par_res': action.update_action,
  63. 'update': [],
  64. 'set': [],
  65. 'where': [],
  66. }
  67. #print('in the update_parse:',par_res)
  68. return hand_parse(par_res, sql_dic)
  69.  
  70. def delect_parse(par_res):
  71. '''
  72. 定义delect_parse函数,接受用户输入的查询sql语句并进行sql解析
  73. :param sql:
  74. :return:
  75. '''
  76. sql_dic = {
  77. 'par_res':action.delect_action,
  78. 'delect': [],
  79. 'from': [],
  80. 'where':[]
  81. }
  82. #print('in the delect_parse:',par_res)
  83. return hand_parse(par_res, sql_dic)
  84.  
  85. def hand_parse(par_res,sql_dic):
  86. '''
  87. 该函数把接受过来的函数进行格式化解析操作,并将整合后的sql字典作为返回值传参给 sql_action()语句主执行函数
  88. :param sql_list:
  89. :param sql_dic:各sql模块所对应的sql语句结构字典
  90. :return:
  91. '''
  92. for item in par_res: #循环遍历select_parse()函数传过来的:用户输入的sql语句
  93. if item in sql_dic: #判断语句中的关键字是否在相应函数sql_dic字典中
  94. key = item
  95. else:
  96. sql_dic[key].append(item) #将字典转化为 select:[*] from:[db.emp] 格式
  97. #print('in the hand_parse:',sql_dic)
  98.  
  99. if sql_dic.get('where'): #整理并格式化where[id < 10...]字段内容
  100. res_list = []
  101. key = ['and','or','not']
  102. char = ''
  103. for item in sql_dic.get('where'):
  104. if len(item) == 0:
  105. continue
  106. if item in key:
  107. if len(char) != 0:
  108. char = where_parse(char) #将char最为实参传参给where_parse()函数。例:char = 'id ','>','10'
  109. res_list.append(char)
  110. res_list.append(item)
  111. char = ''
  112. else:
  113. char += item
  114. else:
  115. char = where_parse(char) ##将char最为实参传参给where_parse()函数。例:char = 'id ','>','10'
  116. res_list.append(char),
  117. sql_dic['where'] = res_list #将where列表内容整理成 where['id > 10','and','id < 20']的格式
  118. #print('where字段列表元素:',sql_dic['where'],sql_dic)
  119. return action.sql_action(sql_dic) #将where['id > 10','and','id < 20']的列表格式作为返回值,
  120. # 传参给where_parse()函数进行最终整理
  121.  
  122. def where_parse(where_char):
  123. '''
  124. 该函数接收hand_parse()传递过来的char参数,并把这些参数整理成['id ','>','10']这样的格式,并返回一个where_res_list值
  125. :param where_char: where_parse(where_char)函数中where_char形参接收的是hand_parse()传递过来的char参数
  126. :return: 把整理完毕的参数格式作为一个 where_res_list列表 的返回值
  127. '''
  128. key = ['<','>','=']
  129. where_res_list = []
  130. opt = ''
  131. char = ''
  132. tag = False
  133. for item in where_char: #循环遍历where_char字符串,如: 'id > 10'
  134. if len(item) == 0:
  135. continue
  136. if item in key:
  137. tag = True #将tag状态变为True
  138. if len(char) != 0 :
  139. where_res_list.append(char)
  140. char = ''
  141. opt += item
  142. if not tag: #判断tag状态是否是False
  143. char += item
  144. if tag and item not in key: #判断tag状态为False并且不在key列表中
  145. tag = False #将tag状态变为False
  146. where_res_list.append(opt)
  147. opt = ''
  148. char += item
  149. else:
  150. where_res_list.append(char)
  151.  
  152. if len(where_res_list) == 1 : # 将列表中'namelikealex'字符串内容转换成['name','like','alex']格式
  153. where_res_list = where_res_list[0].split('like')
  154. where_res_list.insert(1,'like')
  155. #print('in the where_parse:', where_res_list)
  156. return where_res_list

action语句执行模块

  1. #-*- Coding:utf-8 -*-
  2. # Author: D.Gray
  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

5、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

老男孩Day4作业:员工信息查询系统的更多相关文章

  1. python 写的员工信息查询

    #!/use/bin/env pythonn#_*_ coding:utf-8 _*_import timedef Bre():    while True:        Bre_falg = ra ...

  2. C语言身份证信息查询系统(修改版)

    很久以前写了一个<C语言身份证信息查询系统>,如果你点击链接进去看了. 估计也会被我那磅礴大气的代码震惊到的,最近复习/学习文件操作,把代码改了改,算是对以前还不会文件操作的时候的愿望,哈 ...

  3. day12 python作业:员工信息表

    作业要求: 周末大作业:实现员工信息表文件存储格式如下:id,name,age,phone,job1,Alex,22,13651054608,IT2,Egon,23,13304320533,Tearc ...

  4. python作业员工信息表程序(第四周)

    作业需求: 1. 员工信息表程序,实现增删改查操作: 2. 可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22 ...

  5. Day4作业:蛋疼CRM系统

    先上流程图,还得27寸4K显示器,画图各种爽: ReadMe: 运行程序前的提示: 1.抱歉,你得装prettytable模块...... 2.还得抱歉,如果shell中运行,最好把字体调得小点,表格 ...

  6. day4作业之信息表

    实在是太low了,终究是自己写的,记录下 #!/usr/bin/env python # coding=utf8 import os, re #这里我把查询这块分为3个函数了,纠结了很久是放一起还是分 ...

  7. 用户信息查询系统_daoImpl

    package com.hopetesting.dao.impl;import com.hopetesting.dao.UserDao;import com.hopetesting.domain.Us ...

  8. 老男孩Day5作业:电子银行购物商城

    1.作业需求: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录支持账户间转账 记录每月日常消 ...

  9. python-查询员工信息表

    python查询员工信息表 基本要求: 用户可以模糊查询员工信息 显示匹配了多少条,匹配字符需要高亮显示 代码: #!/usr/env python #coding:utf-8 import time ...

随机推荐

  1. Oracle session出现大量的inactive

    一.官网说明 1.1 processes 11gR2 的文档: Property Description Parameter type Integer Default value 100 Modifi ...

  2. Spring 学习一 @Autowired

    @Autowired Spring 2.5 引入了 @Autowired ,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get方 ...

  3. NAND FLASH 驱动分析

    NAND FLASH是一个存储芯片 那么: 这样的操作很合理"读地址A的数据,把数据B写到地址A" 问1. 原理图上NAND FLASH和S3C2440之间只有数据线,       ...

  4. 数据库:sql语句分别按日,按周,按月,按季统计金额

    如: 表:consume_record 字段:consume (money类型) date (datetime类型) 请问怎么写四条sql语句分别按日,按周,按月,按季统计消费总量. 如:1月 120 ...

  5. web基础 (二) html标签

    一.html是什么? 超文本标记语言(Hypertext Markup Language,HTML)通过标签语言来标记要显示的网页中的各个部分.一套规则,浏览器认识的规则 浏览器按顺序渲染网页文件,然 ...

  6. TCP/IP 笔记 7 Ping

    lenovo-myc@lenovomyc-Lenovo-Product:~$ ping www.baidu.com PING www.a.shifen.com (() bytes of data. 这 ...

  7. 用position: sticky 实现粘性元素区域悬浮效果(转)

    用position: sticky 实现粘性元素区域悬浮效果 原创 2017年08月02日 20:04:13 161 在一些很长的表格中,常常会使用表头悬浮的设计以方便阅读,即在表格离开窗口之前,表头 ...

  8. 面试题:hibernate第三天 一对多和多对多配置

    1.1 一对多XML关系映射 1.1.1 客户配置文件: <?xml version="1.0" encoding="UTF-8"?> <!D ...

  9. PCL—点云分割(超体聚类) 低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5013968.html 1.超体聚类——一种来自图像的分割方法 超体(supervoxel)是一种集合,集合的元素是 ...

  10. EZOJ #80

    传送门 分析 经典的树型DP 我们记录dp[i][0/1]表示i的子树中到i的长度分别为偶数和奇数的长度和 dp2[i][0/1]则表示不在i的子树中的点到i的长度分别为偶数和奇数的长度和 然后根据边 ...