staff_table
1,Alex Li,22,13651054608,IT,2013-04-01 2,Jack Wang,30,13304320533,HR,2015-05-03 3,Rain Liu,25,1383235322,Saies,2016-04-22 4,Mmck Cao,40,1356145343,HR,2009-03-01
+---------------------------------------READ ME-------------------------------------------------+ | 注:命令及关键字请使用小写 | 本代码为模拟简单的 MySQL 增删改查的功能,可参考给的例子,自行输入 +-----------------------------------------------------------------------------------------------+ | * 的位置表示显示范围,也可以输入表头名字,只打印这几列 | 范围 的位置,表示查找(修改)的条件,可以用and连接多个参数 | (field1,field2) 表示你增加时需要写入的列的名字, phone 必选 | values(value1,value2) 表示你增加时需要写入的列的数值 +-----------------------------------------------------------------------------------------------+ | 查找:select * from staff_table where 范围 | :select * from staff_table where age > 22 and dept = "HR" and enroll_date like "2009" | :select name,age from staff_table where age > 22 +-----------------------------------------------------------------------------------------------+ | 插入:insert into staff_table (field1,field2) values(value1,value2) | :insert into staff_table (name,age,phone,dept) values ('jia',22,15246512541,'IT') +-----------------------------------------------------------------------------------------------+ | 删除:delete from staff_table where 范围 | :delete from staff_table where staff_id = 3 +-----------------------------------------------------------------------------------------------+ | 更新:update staff_table set field1=value1 where 范围 | :update staff_table set age = 23 where staff_id = 1 +-----------------------------------------------------------------------------------------------+ 退出:q 再看一遍说明:r
low_B的代码
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "Always" 4 # Date: 2017/6/16 5 import time 6 7 file_path = r'staff_table' 8 readme_path = r'readme.txt' 9 all_tittle = ['staff_id', 'name', 'age','phone', 'dept', 'enroll_date'] 10 func_dic = {} 11 12 def add_func_dic(keys): 13 def deco(func): 14 func_dic[keys] = func 15 return deco 16 17 def resolve_cmd(cmd): 18 """ 19 分解命令 20 这个函数,主要是用来处理接收的 select 命令,将各个参数提取出来,放到一个字典中 21 :param cmd: 这个参数是用户输入的,命令,格式为字符串形式 22 :return: 23 """ 24 cmd_dict = {'show_tittle':None, 'file_name':None, 'select_condetion':None, 'cmd':None} 25 cmd_deal = cmd.split('from')[0].split() 26 cmd_dict['cmd'] = cmd_deal.pop(0).strip() 27 cmd_deal = ''.join(cmd_deal).split(',') 28 cmd_dict['show_tittle'] = all_tittle if cmd_deal[0].strip() == "*" else cmd_deal 29 filename_select = cmd.split('from')[1].strip().split('where') 30 cmd_dict['file_name'] = filename_select[0].strip() 31 if len(filename_select) > 1: 32 cmd_dict['select_condetion'] = filename_select[1].strip() 33 return cmd_dict 34 35 def deal_select_condetion(select_condetion): 36 """ 37 主要用来处理后面的条件有 like 的情况,为筛选的条件重新排序 38 :param select_condetion: 39 :return: 40 """ 41 select_condetion = select_condetion.split('and') 42 for i in range(len(select_condetion)): 43 if 'like' in select_condetion[i]: 44 select_condetion[i] = select_condetion[i].split() 45 select_condetion[i].reverse() 46 select_condetion[i] = ' '.join(select_condetion[i]) 47 return ' and '.join(select_condetion).replace('like', 'in') 48 49 50 def return_select_result(total_count, all_tittle, show_tittle, d): 51 """ 52 这个函数主要用来处理打印满足条件的行数和列数 53 :param total_count: 用来计算一共打印了多少行 54 :param all_tittle: 所有的表头,用来固定打印顺序 55 :param show_tittle: 代表需要打印的列 56 :return: 用来计算一共打印了多少行 57 """ 58 for tittle in all_tittle: 59 if tittle in show_tittle: 60 print('%-14s' % d[tittle], end='') 61 print() 62 total_count += 1 63 return total_count 64 65 @add_func_dic('select') 66 def what_a_funck(cmd): 67 cmd_dict = resolve_cmd(cmd) 68 select(cmd_dict['file_name'], cmd_dict['select_condetion'], cmd_dict['show_tittle']) 69 70 def select(file_name, select_condetion, show_tittle): 71 """ 72 这个功能用来查找,并打印符合要求的字段 73 :param file_name 74 :param select_condetion 75 :param show_tittle 76 :return: 77 """ 78 file_info_list = get_file_info_to_list(file_name) 79 80 for tittle in all_tittle: # 打印表头 81 if tittle in show_tittle: 82 print('