python第十五天-原来还差一份作业

作业 1: 员工信息表程序,实现增删改查操作

可进行模糊查询,语法至少支持下面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"
查到的信息,打印后,最后面还要显示查到的条数
可创建新员工纪录,以phone做唯一键,staff_id需自增
可删除指定员工信息纪录,输入员工id,即可删除
可修改员工信息,语法如下:
  UPDATE staff_table SET dept="Market" WHERE where dept = "IT"
注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码

详细描述参考http://www.cnblogs.com/alex3714/articles/5740985.html

代码才写了一半,今天 有公开课就没有写完成!先上一部分,明天再战

  1 #!usr/bin/env python
  2 #-*-coding:utf-8-*-
  3 # Author calmyan
  4 import os ,sys
  5 BASE_DIR=os.path.dirname(os.path.abspath(__file__))#获取相对路径转为绝对路径赋于变量
  6 sys.path.append(BASE_DIR)#增加环境变量
  7 
  8 select_='select'#定义关键字
  9 from_='from'
 10 where_='where'
 11 update_='update'
 12 delete_='delete'
 13 insert_='insert'
 14 values_='values'
 15 into_='into'
 16 set_='set'
 17 
 18 def fj(line):#分解字符串
 19     a=line.split(',')#用 , 分割为列表
 20     dict_list={}#定义一个字典
 21     dict_list[a[3]]={'id':int(a[0]),'name':a[1],'age':int(a[2]),'dept':a[4],'ennoll_date':a[5].strip()}
 22     #print(dict_list)
 23     return dict_list#
 24 
 25 def dict_info(file_name):#员工信息表提取函数
 26     user_dict={}#定义一个员工信息的字典
 27     with open(file_name,'r',encoding='utf-8') as user_info:
 28         for line in user_info:
 29             user_dict.update(fj(line))#更新员工信息的字典
 30     return user_dict
 31 
 32 
 33 def sql_parsing(sql):#sql解析函数 主入口
 34     #sql=sql.lower()
 35     print(sql)
 36     if sql.startswith('select'):#判断语句类型
 37         _dict_=select_par(sql)#调用相关函数
 38     elif sql.startswith('update') or sql.startswith('UPDATE')  :#判断语句类型
 39         _dict_=update_par(sql.lower())
 40     elif sql.startswith('insert'):#判断语句类型
 41         _dict_=insert_par(sql)
 42     elif sql.startswith('delete'):
 43         _dict_=delete_par(sql)
 44     else:
 45         print('输入有误,请重新输入!')
 46     return _dict_#返回解析完成的列表
 47 
 48 
 49 def select_par(sql):#select语句的解析函数
 50     list_key=parsing(sql,index_select=-1,index_from=-1,index_where=-1,select_=select_,where_=where_,from_=from_)#查询调用
 51     return list_key
 52 def update_par(sql):#update语句的解析函数
 53     list_key=parsing(sql,index_update=-1,index_set=-1,update_=update_,set_=set_,where_=where_)#更新修改调用
 54     return list_key
 55 def insert_par(sql):#insert语句的解析函数
 56     list_key=parsing(sql,index_insert=-1,index_into=-1,index_values=-1,insert_=insert_,values_=values_)#添加调用
 57     return list_key
 58 
 59 def delete_par(sql):#delete语句的解析函数
 60     list_key=parsing(sql,index_delete=-1,index_from=-1,index_where=-1,delete_=delete_,where_=where_,from_=from_)#删除调用
 61     return list_key
 62 
 63 
 64 
 65 
 66 def where_format(where_list):#格式化where 子句函数
 67     #print(where_list)
 68     list_format=[]
 69     key_=['and','or','like','not']
 70     str=''#字符连接变量
 71     for i in where_list:
 72         if len(i)==0:continue#如果为空就不连接
 73         if i not in key_:##如果不是关键字就进行连接
 74             str+=i
 75         elif i in key_ and len(str)!=0:
 76             list_format.append(str)#之前的字符串添加到列表
 77             list_format.append(i)#关键 字添加到列表
 78             str=''#清空变量 备用
 79         # if i in key_ and len(str)!=0:#如果循环到关键 字
 80         #     list_format.append(str)#之前的字符串添加到列表
 81         #     list_format.append(i)#关键 字添加到列表
 82         #     str=''#清空变量 备用
 83         # else:#如果不是关键字就进行连接
 84         #     str+=i#字符连接
 85         #     print(str)
 86     list_format.append(str)#最后的字符串
 87     #print(list_format)
 88     return list_format
 89 
 90 def select_format(select_list):#格式化select函数
 91     print(select_list)
 92     #str=''
 93     if select_list[0]=='*':#如果为* 就不改动
 94         pass
 95     else:
 96         # for i in select_list:#列表内容转为字符串
 97         #     str+=i
 98         list_=str_conn(select_list).split(',')#字符串连接函数
 99         #list_=str.split(',')#
100         print(list_)
101     return list_
102 
103 #字符串连接函数
104 def str_conn(list):
105     str=''
106     for i in list:
107         str+=i
108     return str
109 
110 
111 def parsing(sql,**kwargs):#select语句的解析函数
112     list_l=sql.split(' ')#分割存为列表
113     index_from=-1#下标定义from
114     index_select=-1#select
115     index_where=-1#where
116     index_update=-1#update
117     index_set=-1#set
118     index_delete=-1#delete
119     index_insert=-1#insert
120     index_values=-1#valuse
121     list_key={'select':[],'update':[],'delete':[],'insert':[],'set':[],'from':[],'into':[],'where':[],'values':[]}#定义要处理的关键字字典
122     for index,i in enumerate(list_l):#获取下标
123         if i==select_:#如果是关键字select字典下标
124             index_select=index
125         if i==update_:#如果是关键字update字典下标,全部转为小写
126             index_update=index
127         if i==set_:
128             index_set=index
129         if i==insert_:#如果是关键字insert字典下标
130             index_insert=index
131         if i==into_:
132             index_into=index
133         if i==values_:
134             index_values=index
135         if i==delete_:
136             index_delete=index
137         if i==from_:#如果是关键字from字典下标
138             index_from=index
139         if i==where_:#如果是关键字where字典下标
140             index_where=index
141 
142     for index,i in enumerate(list_l):#用下标界定,对进行关键字字典的添加
143         if index_select !=-1 and index>index_select and index<index_from:#在select和from中添加到select中,不为空时
144             list_key[select_].append(i)#添加当前值
145         if index_update !=-1 and index==index_update:#如果是update语句添加一个1值
146             list_key[update_].append(1)
147         if index_insert !=-1 and index==index_insert:#如果是insert语句添加一个1值
148             list_key[insert_].append(1)
149         if index_delete !=-1 and index==index_delete:#如果是delete语句添加一个1值
150             list_key[delete_].append(1)
151         if index_from!=-1 and  index>index_from and index<index_where:#添加from中的值
152             list_key[from_].append(i)
153         if index_set!=-1 and index>index_set and index<index_where:
154             list_key[set_].append(i)
155         if index_where!=-1 and index>index_where:
156             list_key[where_].append(i)
157         if index_values!=-1 and index>index_values:
158             list_key[values_].append(i)
159     if list_key.get(where_):#如果字典在的列表不为空
160         list_key[where_]=where_format(list_key.get(where_))#进行格式化,重新赋于字典,调用格式化函数
161     if list_key.get(select_):#如果字典select在的列表不为空
162         list_key[select_]=select_format(list_key.get(select_))#进行格式化,重新赋于字典
163     if list_key.get(values_):#如果字典values在的列表不为空
164         list_key[values_]=select_format(list_key.get(values_))#进行格式化,重新赋于字典
165     return list_key
166 
167 #执行部分
168 def sql_perform(sql_dict):#执行函数
169     print()
170 
171 def perform_select(sql_dict):#执行select操作的函数
172     select_dict=sql_dict
173     return select_dict#返回处理后的信息
174 
175 def perform_update(sql_dict):#执行update操作的函数
176     select_dict=sql_dict
177     return select_dict#返回处理后的信息
178 
179 def perform_insert(sql_dict):#执行insert操作的函数
180     select_dict=sql_dict
181     return select_dict#返回处理后的信息
182 
183 def perform_delete(sql_dict):#执行delete操作的函数
184     select_dict=sql_dict
185     return select_dict#返回处理后的信息
186 
187 
188 
189 
190 
191 
192 
193 print("33[35;1m欢迎进入员工信息表查询系统33[0m".center(60,'='))
194 if __name__ =='__main__':
195     while True:
196         print('查询修改示例'.center(50,'-').center(60,' '))
197         print('''
198         33[32;1m1、 select name,age from staff_table where age>2233[0m
199         33[31;1m2、 UPDATE staff_table SET dept="Market" WHERE dept="IT"33[0m
200         33[33;1m3、 insert into staff_table values Alex Li,22,13651054608,IT,2013-04-0133[0m
201         33[32;1m3、 delete from staff_table where id=5 33[0m
202         ''')
203         sql=input('sql->').strip()#定义显示提示符
204         if sql=='exit':break#如果输入exit退出
205         if sql==0:continue#如何没输入重新提示
206         sql_dict=sql_parsing(sql)#解析输入的sql语句
207         print(sql_dict)
208         sql_action=sql_perform(sql_dict)#执行解析后的语句
209         file_name='db/staff_table'
210         #print(dict_info(file_name))#传入要查询的表名
211         user_info=dict_info(file_name)#传入要查询的表名,添加到字典
View Code

您的资助是我最大的动力!
金额随意,欢迎来赏!

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

如果,想给予我更多的鼓励,求打

因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【莫柔落切】!

联系或打赏博主【莫柔落切】!https://home.cnblogs.com/u/uge3/

原文地址:https://www.cnblogs.com/uge3/p/6870199.html