【項目要求】
可進行模糊查詢,語法至少支持下面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%%"
可修改員工信息,語法如下:
update staff_table set dept="Market" where dept = "IT"
1. 支持至少三種方法的select查詢,並在最后顯示查詢到的條數。
2. 創建新員工記錄,以phone為唯一鍵,staff_id自增
3. 輸入員工id可刪除指定員工信息記錄
4. 可以使用update命令修改指定員工信息。
2. 創建新員工記錄,以phone為唯一鍵,staff_id自增
3. 輸入員工id可刪除指定員工信息記錄
4. 可以使用update命令修改指定員工信息。
方法一:利用數據庫實現!
具體代碼如下:
1 from sqlalchemy import create_engine 2 from sqlalchemy.ext.declarative import declarative_base 3 from sqlalchemy import Column,Integer,CHAR,VARCHAR,DATE 4 from sqlalchemy.orm import sessionmaker 5 6 #建立鏈接 7 engine = create_engine("mysql+pymysql://root:761379545@127.0.0.1:3306/test_db?charset=utf8", max_overflow=5) 8 Session = sessionmaker(bind=engine) 9 session = Session() 10 Base = declarative_base() 11 class Emp(Base): 12 __tablename__ = 'staff_table' 13 id = Column(Integer,primary_key=True,autoincrement=True) 14 name = Column(VARCHAR(20),nullable=False) 15 age = Column(Integer,nullable=True) 16 phone = Column(CHAR(11),unique=True) 17 dept = Column(VARCHAR(255)) 18 enroll_date = Column(CHAR(20)) 19 # #創建表格 20 # Base.metadata.create_all(engine) 21 22 def search_user(): 23 msg =''' 24 可進行模糊查詢,語法至少支持下面3種: 25 select name,age from staff_table where age > 22 26 select * from staff_table where dept = "IT" 27 select * from staff_table where enroll_date like('2013%%') 28 ''' 29 while 1: 30 print(msg) 31 cmd = input('請輸入要進行操作的語句【返回B】:\n>>>').strip() 32 if not cmd:continue 33 if cmd.lower() == 'b': 34 break 35 try: 36 data = engine.execute(cmd) 37 res= data.fetchall() 38 if not res: 39 print('沒有滿足條件的員工!') 40 else: 41 for row in res: 42 print(row) 43 except Exception: 44 print('請輸入正確的查詢語句!') 45 continue 46 47 def add_user(): 48 ''' 49 新增員工信息 50 ''' 51 while 1: 52 user_name = input('請輸入員工名【返回B】:').strip() 53 if user_name.lower() == 'b': 54 break 55 user_age = input('請輸入年齡:').strip() 56 user_age_int = int(user_age) 57 user_phone = input('請寫入手機號:').strip() 58 user_dept = input('所屬部門').strip() 59 user_enroll_date = input('入職年月日【年-月-日】:').strip() 60 try: 61 session.add(Emp(name=user_name,age=user_age_int,phone=user_phone,dept=user_dept,enroll_date=user_enroll_date)) 62 session.commit() 63 print('員工添加成功!') 64 except Exception: 65 print('員工添加失敗!') 66 continue 67 68 def modify_user(): 69 msg = ''' 70 可修改員工信息,語法如下: 71 update staff_table set dept="Market" where dept = "IT" 72 ''' 73 while 1: 74 print(msg) 75 cmd = input('請輸入要進行操作的語句【返回B】:\n>>>').strip() 76 if not cmd: continue 77 if cmd.lower() == 'b': 78 break 79 try: 80 engine.execute(cmd) 81 print('修改成功!') 82 except Exception: 83 print('請輸入正確的查詢語句!') 84 continue 85 86 def delete_user(): 87 ''' 88 刪除員工信息 89 ''' 90 while 1: 91 del_id = input('請輸入您要刪除的員工ID【返回B】:').strip() 92 if not del_id:continue 93 if del_id.lower() == 'b': 94 break 95 if del_id.isdigit(): 96 del_id_int = int(del_id) 97 try: 98 session.query(Emp).filter(Emp.id==del_id_int).delete() 99 session.commit() 100 print('刪除成功!') 101 except Exception: 102 print('刪除失敗!') 103 continue 104 105 #主程序 106 if __name__ == '__main__': 107 print('歡迎進入員工信息操作界面!'.center(23,'*')) 108 while 1: 109 choice = input('請輸入想要進行的操作代碼:' 110 '\n\t【1】查詢員工信息' 111 '\n\t【2】新增員工信息' 112 '\n\t【3】修改員工信息' 113 '\n\t【4】刪除員工信息' 114 '\n\t【Q】退出操作' 115 '\n>>>').strip() 116 if choice == '1': 117 search_user() 118 elif choice == '2': 119 add_user() 120 elif choice == '3': 121 modify_user() 122 elif choice == '4': 123 delete_user() 124 elif choice.lower() == 'q': 125 print('退出成功,感謝您的使用!'.center(22,'*')) 126 break 127 else: 128 print('您的操作有誤,請重新選擇!')
方法二:函數加文件讀寫實現(有待完善)!
1 import os 2 def inquiry_system(): 3 welcome =''' 4 -----------員工信息查詢系統------------ 5 操作說明: 6 增加ADD:1 7 刪除DELETE:2 8 修改CHANGE:3 9 查詢SEARCH:4 10 退出QUIT:5 11 --------------------------------------- 12 ''' 13 print(welcome) 14 15 def add(): 16 # 添加語法: name,age,phone,dept,enroll-date 17 add_staff =input('請輸入要添加的員工信息:(添加格式: name,age,phone,dept(大寫),enroll-date)\n') 18 list_data =add_staff.split(',') 19 list_all =[] 20 f = open('staff_table.txt','r+',encoding='utf8') 21 for line in f: 22 list_all.append(line.strip().split(',')[3]) 23 if list_data[2] in list_all: 24 print('用戶已存在!') 25 f.close() 26 else: 27 for line in f: 28 f.write(line) 29 staff_id = str(len(list_all) + 1) 30 list_data.insert(0, str(staff_id)) 31 f.write('\n') 32 f.write(','.join(list_data)) 33 f.close() 34 print('添加成功') 35 36 def delete(): 37 del_staff_id =input('請輸入要刪除的員工ID:') 38 f = open('staff_table.txt','r',encoding='utf8') 39 f1 = open('new_staff_table.txt','w',encoding='utf8') 40 for line in f: 41 each_line = line.split(',') 42 if int(each_line[0]) != int(del_staff_id): 43 f1.write(line) 44 #若想實現刪除后員工ID順序排列使用下三行代碼! 45 # elif int(each_line[0]) > int(del_staff_id): 46 # each_line[0] =str(int(each_line[0])-1) 47 # f1.write(','.join(each_line)) 48 else: 49 continue 50 f.close() 51 f1.close() 52 os.remove('staff_table.txt') #刪除指定目錄 53 os.rename('new_staff_table.txt', 'staff_table.txt') #重命名 54 55 def change(): 56 # 修改請輸入(注意空格和沒有引號):UPDATE staff_table SET dept = IT where dept = MARKET 57 data = input("請輸入您要修改的信息:(修改格式:UPDATE staff_table SET dept = IT where dept = MARKET)\n") 58 old = data.split(' ')[5] 59 new = data.split(' ')[9] 60 f = open('staff_table.txt', 'r', encoding='utf-8') 61 f1 = open('new_staff_table.txt', 'w', encoding="utf-8") 62 for line in f: 63 if old in line: 64 line = line.replace(old, new) 65 f1.write(line) 66 f.close() 67 f1.close() 68 os.remove('staff_table.txt') 69 os.rename('new_staff_table.txt', 'staff_table.txt') 70 print('修改成功') 71 72 def search(): 73 formation=''' 74 查詢方式1:(按年齡查詢)select * from staff_table where age >= 22 75 查詢方式2:(按職業查詢)select * from staff_table where dept = "IT" 76 查詢方式3:(按年份查詢)select * from staff_table where enroll_date like "2013" 77 ''' 78 print(formation) 79 80 while True: 81 search_way = input('請輸入您的查詢方式(退出Q):') 82 res = [] 83 count = 0 84 if search_way.lower() =='q': 85 exit() 86 if search_way =='1': 87 age_search = input('請輸入要查詢的最小年齡:') 88 with open('staff_table.txt','r',encoding='utf8') as f: 89 for line in f: 90 each_line =line.split(',') 91 if int(each_line[2]) >=int(age_search): 92 res.append(line) 93 count += 1 94 print("查詢的結果為:") 95 for per in res: 96 print(per) 97 print('共有%s個員工滿足查詢信息條件!'%count) 98 break 99 if search_way =='2': 100 dept_search = input('請輸入要查詢的職業:') 101 with open('staff_table.txt', 'r', encoding='utf8') as f: 102 for line in f: 103 each_line = line.split(',') 104 res.append(each_line) 105 result =filter(lambda l:l[4]==dept_search.upper(),res) 106 for i in result: 107 count+=1 108 print(i) 109 print('共有%s個員工滿足查詢信息條件!'%count) 110 break 111 if search_way =='3': 112 enroll_date = input('請輸入要查詢的年份:') 113 with open('staff_table.txt', 'r', encoding='utf8') as f: 114 for line in f: 115 each_line = line.split(',') 116 res.append(each_line) 117 result = filter(lambda l:enroll_date in l[5],res) 118 for i in result: 119 count += 1 120 print(i) 121 print('共有%s個員工滿足查詢信息條件!' %count) 122 break 123 else: 124 print('請重新輸入!') 125 126 my_choice = { 127 '1':add, 128 '2':delete, 129 '3':change, 130 '4':search, 131 '5':'quit' 132 } 133 while True: 134 inquiry_system() 135 choice = input('請輸入操作序號:') 136 if choice not in my_choice: 137 print("輸入錯誤,請重新輸入!") 138 continue 139 if int(choice) == 5: 140 exit() 141 else: 142 my_choice[choice]()