ImportOslist1= ['staff_id','name',' Age','Phone','Dept','enroll_date']defstaff_info ():#Get Employee information generator functionWith open ('Staff_table.txt','R', encoding='Utf-8') as F: forLineinchf:x=line.split (',') Staff_dic= {K:v for(k, V)inchZip (list1,x)}yieldStaff_dicdefSelect_func (select_content,start,middle,end):#Find function ifMiddle = ='=': Middle='==' ifSelect_content = ='*': Content=List1Else: Content= Select_content.split (',') Total_check=0 forIinchstaff_info (): List= [] forJinchcontent:list.append (I[j])#find items that need to be listed ifMiddle = =' like': L=Len (end)ifEval (I[start][:l] +'=='+end):Print(list) Total_check+=1Else: S=I[start] e=EndifEval"s"+middle+"e"): Print(list) Total_check+=1Print('related queries total%s'%Total_check)defInsert_func (list):#Add function function, add content to write in listWith open ('Staff_table.txt','r+', encoding='Utf-8') as F:#determine if the phone number is repeated, if repeated error exits forLineinchF:ifLIST[2]inchLine :Print('Err Information') return #add content that is not duplicatedF.seek (0) Info=f.readlines () staff_id=info[-1].split (',') staff_id=int (staff_id[0]) +1f.seek (0,2) s="' forIinchlist:ifType (i) = =int:i=Str (i) s=s+','+i f.write (str (staff_id)+s+'\ n')defDelete_func (staff_id):#Delete Information functionWith open ('Staff_table.txt','r+', encoding='Utf_8') as F, open ('New.txt','W', encoding='Utf-8') as F1: forLineinchF:ifstaff_id = =line[0]: Line="'F1.write (line) Os.remove ('Staff_table.txt') Os.rename ('New.txt','Staff_table.txt')defModify_func (oldcontent,newcontent):#Modifying FunctionsWith open ('Staff_table.txt','r+', encoding='Utf_8') as F, open ('New.txt','W', encoding='Utf-8') as F1: forLineinchF:ifOldcontentinchLine:line=line.replace (oldcontent,newcontent) f1.write (line) Os.remove ('Staff_table.txt') Os.rename ('New.txt','Staff_table.txt')defRun_func ():#Run function while1: cmd=input ('>>>:') ifcmd = ='quit': Break Else: cmd=Cmd.split ()ifCmd[0] = ='Select': End=cmd[-1] Start=cmd[-3] Middle=cmd[-2] Select_content=cmd[1] Select_func (select_content,start,middle,end)elifCmd[0] = ='Insert': List=cmd[1].split (',') Insert_func (list)elifCmd[0].lower () = ='Update': Oldcontent=cmd[-1] Newcontent=cmd[5] Modify_func (oldcontent,newcontent)elifCmd[0] = ='Delete': staff_id=cmd[1] Delete_func (staff_id)Else: Print('Err Input') Run_func ()
Contents of Staff_table.txt:
1,alex li,22,13651054608,it,2013-04-01
2,jack wang,30,13304320533,hr,2015-05-03
3,rain liu,25,1383235322,sales,2016-04-22
4,mack cao,40,1356145343,hr,2009-03-01
Due to limited capacity, the command format should follow the following instructions
- Query command format: Select Age,name from satff_table where dept = = it/age > 20/other conditions
- Modify command format: Update staff_table Set dept = Market where where dept = IT
- Add command format: Insert Mona, 13693041938, IT, 2016-09-04
- Delete command format: Delete 1
Python function simulates MySQL additions and deletions function