員工信息表程序,實現增刪改查操做: 可進行模糊查詢,語法至少支持下面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 dept = "IT" 注意:以上需求,要充分使用函數,請盡你的最大限度來減小重複代碼
staff_table (文本文件)python
1 小名 21 13311022222 IT 2013-01-05 2 小紅 23 13311033333 Market 2014-01-05 3 小童 25 13311044444 Market 2014-01-05 4 小張 27 13311055555 Market 2015-01-05
start.py (啓動文件)git
import os import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) import conf,core from core import main main.main()
main.py (code文件)sql
#!/use/bin/env python # -*- coding:utf-8 -*- # Author:LT import os BASE_DATA = os.path.dirname(__file__) BASE_DIR = os.path.join(BASE_DATA,'staff_table') def sql_select(): ''' 添加數據前檢測staff_table是否爲空 :return: ''' with open(BASE_DIR, "r+", encoding="utf-8") as f: line = f.read().strip() return line def select(): # 查詢員工信息 ''' 實現三條sql語句的查詢方式 :return: ''' msg = ''' 請輸入一下三種查詢方式: 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" ''' print(msg) user_choice = input("請輸入您要查詢的內容,輸入b返回首頁>>>:").strip() user_choice_list = user_choice.split() # 寫入列表 count = 0 if user_choice == "select name,age from staff_table where age > %s"%user_choice_list[-1]: #判斷是那條查詢語句 with open(BASE_DIR, "r", encoding="utf-8") as f: for line in f: if line =="\n": # 跳過文本中最後一行空格 continue if line.split()[2] > user_choice_list[-1]: #判斷數據庫中大於用戶輸入的age count =count+1 # 獲取到的結果計數 print(line.split()[1],line.split()[2]) print("您查到了",count,"條記錄") return select() elif user_choice == "select * from staff_table where dept = %s" %user_choice_list[-1]: with open(BASE_DIR, "r", encoding="utf-8") as f: for line in f: if line =="\n": continue if line.split()[4] == user_choice_list[-1].split('"')[1]: count = count + 1 print(line.strip()) print("您查到了", count, "條記錄") return select() elif user_choice == "select * from staff_table where enroll_date like %s" %user_choice_list[-1]: with open(BASE_DIR, "r", encoding="utf-8") as f: for line in f: if line =="\n": continue # 判斷文本中的時間和用戶查找的是否相等,相等輸出結果 if line.split()[5].split("-")[0] == user_choice_list[-1].split('"')[1].split("-")[0]: count = count + 1 print(line.strip()) print("您查到了", count, "條記錄") return select() elif user_choice == "b" or user_choice =="B": return main() else: print("請輸入正確的查詢命令") return select() def update(): # 修改員工信息 ''' 修改部門名稱 :return: ''' msg = ''' 請輸入一下命令修改部門名稱: UPDATE staff_table SET dept="Market" where dept = "IT" ''' print(msg) user_choice = input("請輸入修改命令,按b返回首頁>>>:").strip() if user_choice == "b" or user_choice =="B": return main() new_dept = user_choice.split("=")[1].split()[0] # 獲取第一個等於號後" "的內容(包括"") old_dept = user_choice.split("=")[-1] # 獲取第二個等於號後" "的內容(包括"") if user_choice == "UPDATE staff_table SET dept=%s where dept =%s"%(new_dept,old_dept): staff_table_bak = os.path.join(BASE_DATA, 'staff_table_bak') with open(BASE_DIR, "r", encoding="utf-8") as f,open(staff_table_bak, "w", encoding="utf-8") as fs: for i in f: # 循環staff_table文件內容 if i =="\n": continue if old_dept.split('"')[1] in i.split()[4]: # 判斷用戶輸入內容是否在staff_table文件中 i=i.replace(old_dept.split('"')[1],new_dept.split('"')[1]) # 修改staff_table文件內容 fs.write(i) f.close() fs.close() os.remove(BASE_DIR) # 刪除原文件staff_table os.rename(staff_table_bak,BASE_DIR) # 新文件更名爲staff_table print("修改爲功") def add(): # 添加新員工 ''' 添加新員工電話惟一用戶輸入重複的話須要從新輸入新員工信息 :return: ''' name = input("請輸入員工姓名:") age = input("請輸入員工年齡:") phone = input("請輸入員工電話:") dept = input("請輸入員工部門:") times = input("請輸入員工入職時間:") with open(BASE_DIR, "r+", encoding="utf-8") as fs: lines = fs.readlines() if sql_select() == "": # 判斷是否有數據沒有的話第一條記錄ID爲1 user_info = ''' 1 %s %s %s %s %s ''' %(name,age,phone,dept,times) userinfo = user_info.strip() fs.write(str(userinfo + '\n')) print("添加成功") else: tail = lines[-1].split() # 獲取最後一條記錄 staff_id = int(tail[0])+1 # 最後一條記錄ID加1,爲新的記錄ID user_info = ''' %s %s %s %s %s %s ''' %(staff_id,name,age,phone,dept,times) for i in lines: if phone in i.split()[3]: # 判斷手機號是否存在 print("此手機號已存在,請從新輸入員工信息") return add() else: userinfo = user_info.strip() fs.write(str(userinfo + '\n')) print("添加成功") break def delete(): # 刪除員工 ''' 輸入員工ID刪除員工 :return: ''' print(sql_select()) user_choice = input("請輸入要刪除的員工ID,按b返回首頁>>>:").strip() if user_choice == "b" or user_choice =="B": return main() staff_table_bak = os.path.join(BASE_DATA, 'staff_table_bak') with open(BASE_DIR, "r", encoding="utf-8") as f, open(staff_table_bak, "w", encoding="utf-8") as fs: for line in f: if user_choice in line.split()[0]: continue fs.write(line) f.close() fs.close() os.remove(BASE_DIR) os.rename(staff_table_bak,BASE_DIR) print("刪除成功") def main(): while True: user_chioce = ''' 1.查詢員工信息 2.修改員工信息 3.添加新員工 4.刪除員工 5.退出 ''' print(user_chioce) user_choice = input("請輸入你的選擇:") if user_choice.isdigit(): # 判斷輸入的是否數字類型 if user_choice == '1': select() # 查詢 elif user_choice == '2': update() # 修改 elif user_choice == '3': add() # 添加 elif user_choice == '4': delete() # 刪除 elif user_choice == '5': exit("再見") # 退出 else: print("請輸入正確的選項") else: print("請輸入正確的選項")