1. 程式人生 > >python第十五天-原來還差一份作業

python第十五天-原來還差一份作業

!= 絕對路徑 字典 增刪改查 .cn 類型 lose 修改 view

作業 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("\033[35;1m歡迎進入員工信息表查詢系統\033[0m".center(60,=)) 194 if __name__ ==__main__: 195 while True: 196 print(查詢修改示例.center(50,-).center(60, )) 197 print(‘‘‘ 198 \033[32;1m1、 select name,age from staff_table where age>22\033[0m 199 \033[31;1m2、 UPDATE staff_table SET dept="Market" WHERE dept="IT"\033[0m 200 \033[33;1m3、 insert into staff_table values Alex Li,22,13651054608,IT,2013-04-01\033[0m 201 \033[32;1m3、 delete from staff_table where id=5 \033[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

python第十五天-原來還差一份作業