python做系统查人的往来的信息_python实战演练(六)员工信息查询系统

1 #-*- Coding:utf-8 -*-

2 #Author: kking

3 importos,sys,re4

5 #语句主分配模块

6 defsql_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 defselect_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 inselect_res:35 print("查询结果为: \033[34;1m%s\033[0m"%record)36 print('查询结果数量为: \033[34;1m%s\033[0m'%len(record))37 returnselect_res38

39 defwhere_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 infh:51 db_dic = dict(zip(title.split(','), item.split(',')))52 '''

53 定义db_dic函数以字典形式将emp文件中的内容为值,title54 为字典中的键做下拉链拼接。例: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 returnwhere_action_res69

70 deflogic_action(where_l,db_dic):71 '''

72 该函数处理where部分与db.emp文件中的信息进行逻辑分析和对比。并将对比结果为True的信息返回给where_action73 :param where_l:74 :param db_dic:75 :return:76 '''

77 logic_action_res =[]78 for exp inwhere_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 indic_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 returnlogic_action_res100

101 deflimit_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_res108 returnlimit_res109

110 defselect(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 inlimit_res:125

126 for index initem:127 select_dic = dict(zip(title.split(','),index.split(',')))128 exp = select_l[0].split(',')129 for i inexp:130 select_list.append(select_dic[i].strip())131 else:132 select_list =limit_res133 else:134 print('\033[31;1m请输入有效select * 语句\033[0m')135 returnexp,select_list136

137 #insert语句执行模块

138 definsert_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 returnvalue_res154

155 defvalue_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 invalue_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 =True171 if index_l[2] in phone: #以手机号作唯一键,判断用户输入的value值是否存在数据文件中

172 tag =False173 tag_res = print('\033[31;1m该用户已存在不能重复添加\033[0m')174 if tag and len(index_l) < 5:175 tag =False176 tag_res = print('\033[31;1m用户输入value信息不够\033[0m')177 iftag: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 returntag_res,index_l183

184 #update语句执行模块

185 defupdate_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 returnset_res197

198 defset_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 inwhere_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 infh_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 returnset_list235

236 #delect语句执行模块

237 defdelect_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 returndelect_res256

257 defdelect(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 inwhere_res:268 for i ini: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 inlines: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值