day3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day3相关的知识,希望对你有一定的参考价值。
day3作业:
准备工作:创建目录db,创建文件db。
# select name from db.emp where id > 3; #select name,phone from db.emp where id < 3; def sql_parse(sql): ‘‘‘ 解析sql命令字符串,切分,提取命令,判断分给下面4个函数 :param sql: :param key_lis: :return: ‘‘‘ parse_func={ ‘insert‘:insert_parse, ‘delete‘:delete_parse, ‘update‘:update_parse, ‘select‘:select_parse, } sql_l=sql.split(‘ ‘) func=sql_l[0] res=None if func in parse_func: res=parse_func[func](sql_l) # if sql_l[0] in [‘select‘,‘insert‘,‘update‘,‘delete‘]: # res=sql_l() return res def select_parse(sql_l): ‘‘‘ 解析select命令字符串,执行解析sql函数,得到字典sql_dic :param sql: :param key_lis: :return: ‘‘‘ sql_dic={ ‘func‘:select, ‘select‘:[], ‘from‘:[], ‘where‘:[], ‘limit‘:[], } return handle_parse(sql_l,sql_dic) pass def insert_parse(sql_l): ‘‘‘ 解析insert命令字符串,执行解析sql函数,得到字典sql_dic :param sql: :param key_lis: :return: ‘‘‘ sql_dic={ ‘func‘:insert, ‘insert‘:[], ‘into‘:[], ‘values‘: [], } return handle_parse(sql_l,sql_dic) pass def update_parse(sql_l): ‘‘‘ 解析updata命令字符串,执行解析sql函数,得到字典sql_dic :param sql: :param key_lis: :return: ‘‘‘ sql_dic={ ‘func‘:update, ‘update‘:[], ‘set‘:[], ‘where‘:[], } return handle_parse(sql_l,sql_dic) pass pass def delete_parse(sql_l): ‘‘‘ 解析delete命令字符串,执行解析sql函数,得到字典sql_dic :param sql: :param key_lis: :return: ‘‘‘ sql_dic={ ‘func‘:delete, ‘delete‘:[], ‘from‘:[], ‘where‘:[], } return handle_parse(sql_l, sql_dic) pass def handle_parse(sql_l,sql_dic): ‘‘‘ 解析sql函数,得到字典sql_dic :param sql: :param key_lis: :return: ‘‘‘ #取sql_dic的key,即字段名 在sql_l列表中的索引,生成字典 k_i=dic_lis_id(sql_l, sql_dic) #将sql_l切片,追加到sql_dic的value的[]中 for x,y in k_i.items(): sql_dic[x].append(sql_l[y[0]+1:y[1]]) #去除sql_dic的value中的外层[],[[....]] for x,y in sql_dic.items(): if isinstance(y, list) : if not y==[]: sql_dic[x]=y[0] return sql_dic # return dic_lis_id(sql_l,sql_dic) # return print(sql_l,sql_dic) def dic_lis_id(x, y): ‘‘‘取关键字对应索引,通过索引截取关键字后内容‘‘‘ k_i = {} for i in y.keys(): if i in x: k_i[i] = [x.index(i), ] k_i[‘ending‘] = [None,] for o, u in k_i.items(): if u == [0]: o1 = o continue k_i[o1].append(u[0]) o1 = o k_i.pop(‘ending‘) return k_i def select(sql_dic): path=file_path(sql_dic,‘from‘) # print(path) with open(path,‘r‘,encoding=‘utf-8‘) as file: line_l=file.readlines() # print(line_l) col_dic = {‘id‘: [], ‘name‘: [], ‘age‘: [], ‘phone‘: [], ‘job‘: [], ‘date‘: []} file.seek(0) for i in file: # if i == ‘\n‘: continue # print(col_dic.keys(),i.split(‘,‘)) # print() dicr = ki(col_dic.keys(), i.split(‘,‘)) # print(dicr) # for colk,col1 in ki(col_dic.keys(),i.split(‘,‘)).items(): for colk, col1 in dicr.items(): # print(colk,col1) col_dic[colk].append(col1) # print(col_dic) # for i in col_dic[sql_dic[‘where‘][0]]: # if i sql_dic[‘where‘][1] sql_dic[‘where‘][2]: ind_list=get_index(sql_dic[‘where‘],col_dic) # print(ind_list) for i in ind_list: print(‘\033[42;1m%s\033[0m‘ %(line_l[i]),end=‘‘) return ‘\nselect sucess‘ pass def insert(sql_dic): ‘‘‘获取文件路径‘‘‘ path=file_path(sql_dic, ‘into‘) # print(path) with open(path, ‘a‘, encoding=‘utf-8‘) as file: file.write(sql_dic[‘values‘][0]) return ‘insert success‘ def delete(sql_dic): ‘‘‘获取文件路径‘‘‘ path=file_path(sql_dic, ‘from‘) line1=int(where_parse(sql_dic))-1 # print(line1) with open(path, ‘r‘, encoding=‘utf-8‘) as file: line_l = file.readlines() line_l.pop(line1) with open(path, ‘w‘, encoding=‘utf-8‘) as file: for i in line_l: file.write(i) return ‘delete success‘ def update(sql_dic): ‘‘‘获取文件路径‘‘‘ path=file_path(sql_dic, ‘update‘) line1=int(where_parse(sql_dic))-1 # print(line1) with open(path, ‘r‘, encoding=‘utf-8‘) as file: line_l = file.readlines() print(line_l[line1]) listtmp=line_l[line1].split(‘,‘) listtmp.pop(-2) listtmp.insert(-1,sql_dic[‘set‘][-1]) line_l.pop(line1) line_l.insert(line1,‘,‘.join(listtmp)) with open(path, ‘w‘, encoding=‘utf-8‘) as file: for i in line_l: file.write(i) return ‘update success‘ def file_path(sql_dic,fromm): ‘‘‘获取文件路径‘‘‘ return sql_dic[fromm][0].replace(‘.‘,‘\\‘) pass def where_parse(sql_dic): return sql_dic[‘where‘][-1] def get_index(lwhere,col_d): ‘‘‘where字典vlaue列表和总列字典,的索引‘‘‘ resil=[] #比较范围 list=col_d[lwhere[0]] #比较值 val=lwhere[2] #符号判断,运算 if lwhere[1] == ‘=‘: for i in list: if i in val: resil.append(list.index(i)) elif lwhere[1] == ‘>‘: for i in list: if i > val: resil.append(list.index(i)) elif lwhere[1] == ‘<‘: for i in list: if i < val: resil.append(list.index(i)) if lwhere[1] == ‘like‘: for i in list: if val in i: resil.append(list.index(i)) return resil def ki(x,y): ‘‘‘两个同长度的列表,组成一个字典‘‘‘ a,b,kid=[],[],{} for i in x: a.append(i) for i in y: b.append(i) for o in range(len(x)): kid[a[o]]=b[o] return kid if __name__=="__main__": while True: sql = input(‘sql >‘).strip() if sql_parse(sql)==None:continue # print(sql_parse(sql)) # if not sql_parse(sql)[‘func‘] in [select,‘insert‘,‘update‘,‘delete‘,]:continue print(sql_parse(sql)[‘func‘](sql_parse(sql)))
以上是关于day3的主要内容,如果未能解决你的问题,请参考以下文章