staff information and change proceduresFunction:
Can be used for fuzzy query
find name,age from staff_table where age > 22find name from staff_table where dept = "IT"find name from staff_table where id=4find age from staff_table where name = "Alex Li"find * from staff_table where name = "Alex Li"find name,age,phone from staff_table where enroll_date like "2013-04-01"
A new employee record can be created, with the phone making a unique key (that is, it is not allowed to repeat the phone number in the table), sta?_id self-increment
add staff_table Alex Li,25,134435344,IT,2015-10-29
Delete a specified employee information record, enter an employee ID, and delete
del from staff_table where id=4del from staff_table where age>22
Employee information can be modified
UPDATE staff_table SET dept="Market" WHERE dept = "IT"UPDATE staff_table SET age=25 WHERE name = "Alex Li"
After each of these words has been executed, it shows how many records this statement affects. For example, the query statement shows how many queries, modify the statement shows how many modified.
The table Form is as follows:
id |
name |
age |
phone |
dept |
enroll_date |
1 |
Alex Li |
22 |
13651054608 |
IT |
2013-04-01 |
2 |
Jack Wang |
28 |
13451024608 |
HR |
2015-01-07 |
3 |
Rain Wang |
21st |
13451054608 |
IT |
2017-04-01 |
The file is stored in the following ways:
1,Alex Li,22,13651054608,IT,2013-04-012,Jack Wang,28,13451024608,HR,2015-01-073,Rain Wang,21,13451054608,IT,2017-04-014,Mack Qiao,44,15653354208,Sales,2016-02-015,Rachel Chen,23,13351024606,IT,2013-03-166,Eric Liu,19,18531054602,Marketing,2012-12-017,Chao Zhang,21,13235324334,Administration,2011-08-088,Kevin Chen,22,13151054603,Sales,2013-04-019,Shit Wen,20,13351024602,IT,2017-07-0310,Shanshan Du,26,13698424612,Operation,2017-07-02
Code:
#!/usr/bin/env python#-*-coding:utf-8-*-import reimport osdata = {} # holds tabular data tables = ' match = {# header correspondence ' ID ': 0 , ' name ': 1, ' age ': 2, ' phone ': 3, ' dept ': 4, ' Enroll_date ': 5} ' ' Test statement: Find Name,age from staff_table where Age > 22find name from staff_table where dept = "IT" The Find name from Staff_table where Id=4find the from staff_table whe Re name = "Alex Li" Find * from staff_table where name = "Alex Li" Find name,age,phone from staff_table where Enroll_date Li Ke "2013-04-01" find name,age,phone,enroll_date from staff_table where enroll_date like "" "Add staff_table Alex li,25,1 34435344,it,2015-10-29del from staff_table where Id=4del from staff_table where age>22update staff_table SET dept= "Mar Ket "WHERE dept =" IT "UPDATE staff_table SET age=25 WHERE name =" Alex Li "" Def Main (): "Main program projects: What needs to be output Tables: Table for this operation (file name) rule: Data filter condition: return: "While True:global tables global data t Ables = ' data = { ' ID ': [], ' name ': [], ' age ': [], ' phone ': [], ' dept ': [], ' Enroll_date ': []} inputs = input (' >>> '). Strip () handle = Inputs.split (") [0] If Handle = = ' Find ': inputs_list = Inputs.split (' from ') projects = Inputs_list[0].split (") [1].spli T (', ') # print content Inputs_list1 = Inputs_list[1].split (' where ') tables = inputs_list1[0] # table Rule = inputs_list1[1].replace (' = ', ' = = ') Db_open () prints (projects,find (rule)) Elif hand Le = = ' Add ': inputs_list = Inputs.split (', 2) tables = Inputs_list[1] Db_open () Add (inputs_list) elif handle = = ' del ': inputs_list = Inputs.split (' from ') [1].split (' where ') tables = inputs_list[0] rule = inputs_list[1].replace (' = ', ' = = ') db_open () Delete ( Find (rule)) elifhandle = = ' UPDATE ': inputs_list = inputs.split (' SET ') tables = Inputs.split (') [1] upd Ates = Inputs_list[1].split (' where ') [0].replace (' = ', ' = = ') rule = inputs_list[1].split (' where ') [1].replace ( ' = ', ' = = ') print (tables) db_open () Update (find (rule), updates) Else:pri NT (' \033[0;33mplease input:\n ' Find [Id,name,...] from [table] where [Id,name,...] [>\<\=\like] [Id,name,...] \ n ' Add [table] [Name,age,...] \ n ' del from [table] [Id,name,...] where [id,name,...] [>\<\=\like] [Id,name,...] \ n ' UPDATE [table] set [id,name,...] = [Id,name,...] WHERE [Id,name,...] [>\<\=\like] [Id,name,...] \033[0m ') continuedef db_loads (loads): "Loads data into the database dictionary, the data dictionary is used to hold tabular:p Aram loads: The contents of each line read by the file: RE Turn: "If Loads:staff_id,name,age,phone,dept,enroll_date=loads.strip (). Split (', ') # pRint (staff_id,name,age,phone,dept,enroll_date) data[' id '].append (int (staff_id)) data[' name '].append (name) data[' age '].append (int) data[' phone '].append (int (phone)) data[' dept '].append (dept) data[' en Roll_date '].append (enroll_date) def db_open (): "' Open file, read content, call Db_loads (loads): return: ' with open (tables , ' R ', encoding= ' Utf-8 ') as F:for line in F:db_loads (Line.strip ()) def db_save_a (data): "File Append Write :p Aram Data:: Return: ' with open (tables, ' A ', encoding= ' Utf-8 ') as F:f.write (' \033[0;3 3mdata has been saved ... \033[0m ') def find (rule): ' Find qualifying ID:p Aram rule: Data filter condition: return: id "in list form ID = [] if ' like ' in Rule:key = Rule.split (') cmd = "%s in item"% (Key[-1]) Else:key = Re. FindAll (' \w+ ', rule) cmd = Rule.replace (key[0], ' item ') for Index,item in Enumerate (Data[key[0]]): EXEC (' "If%s:id.appenD (Index) "%cmd" Print (' \033[0;33mfind%s pieces of information ... \033[0m '%len (ID)) return iddef Add (inputs _list): "Data increment operation, call db_save_a append content separately, db_loads load new data to data dictionary:p Aram Inputs_list: The data to be added: return: ' If int ( Inputs_list[2].split (', ') [2]) in data[' Phone ": print (' \033[0;33mthe phone number is the same as the others...\033[ 0m ') Else:add_data = str (len (data[' id ')) +1) + ', ' +inputs_list[2]+ ' \ n ' db_save_a (add_data) db_loads ( Add_data) def delete (lists): "Delete operation:p Aram lists: The data to be deleted: return: ' with open (tables, ' r+ ', encoding= ' Utf-8 ') as F, open (' Data_new.txt ', ' W ') as F2:i,j = All-in-line in F:if Int (line.split (', ') [0 ]) not in the list (map (lambda x:x+1,lists)): line = Line.replace (str (j), str (i), 1) F2.write (Lin e) i + = 1 J + = 1 os.remove (tables) os.rename (' data_new.txt ', tables) print (' \033[0;33m. . \ndelete Success!\033[0m ') def update (lists,updates): "Data change operation:p Aram lists: List of filtered data IDs:p Aram updates: Data that needs to be updated: return:" Key = Re.findall (' \w+ ', updates) with open (tables, ' r+ ', encoding= ' utf-8 ') as F, open (' Data_new.txt ', ' W ') as F2: For line in F:if Int (line.split (', ') [0]) in list (map (lambda x:x + 1, lists)): TEM = LINE.S Plit (', ') tem[match[key[0]]] = key[1] line = ', '. Join (TEM) f2.write (line) OS.R Emove (Tables) os.rename (' data_new.txt ', tables) print (' \033[0;33m%s changes ... \033[0m '%len (lists)) def prints (projec Ts,data_num): ' Content output print module:p Aram projects: What needs to be printed:p Aram Data_num: List of data IDs: return: ' tem = ' Tem1 = ' if ' * ' in projects:projects = Data.keys () for i in Projects:tem1 + = "'%s '. Center (11, '), ' | ', "%i tem + =" str (data['%s '][j]). Center (one, "), ' | ',"%i eval ("Print (" + tem1 + ")") for J in Data_num: Print ('-------'. Center (+, ') *len (projects)) eval ("Print (" + tem + ")") if __name__ = = ' __main__ ': Main ()
Python practice employee information and additions to the program