员工信息增删改查程序

# -*- coding:utf-8 -*-
import os

staff_db = 'staff_table.txt'
staff = 'staff_table'
staff_order = ['id', 'name', 'age', 'phone', 'dept', 'enroll_date']

#返回list_data=common_data
def print_search(list_data):
if len(list_data) != 0:
print('影响了33[1;31m%s33[0m条记录'% len(list_data)) #高亮度打印list_data数据长度
for i in list_data: #逐条遍历list_data 并打印
print(i)
else:
print("没有对应得数据!")


def syntax_update(con_sen,common_data):
# update 修改语句
if len(common_data) == 0:
print('没有记录')
return common_data
else:
index_num = 0
id_data = []
new_data = []
for index, i in enumerate(staff_order):
if i == con_sen[0].strip():
index_num = index
break
for index, i in enumerate(common_data):
id_data.append(i.split(',')[0])
f = open(staff_db, 'r+', encoding='utf-8')
f_new = open('staff_table_new.txt', 'w', encoding='utf-8')
for line in f:
temp_li =line.split(',')
if temp_li[0] in id_data:
if con_sen[1].strip().isdigit():
temp_li[index_num] = con_sen[1].strip()
else:
temp_li[index_num] = eval(con_sen[1].strip())
line = ','.join(temp_li)
new_data.append(line.strip())
f_new.write(line)
f.close()
f_new.close()

os.replace(f_new.name, f.name)
return new_data


def syntax_add(add_data):
# add 语句增加
f = open(staff_db, 'r+', encoding='utf-8')
id_data, phone, new_list = [], [], []
for index, line in enumerate(f):
id_data.append(line.split(',')[0])
phone.append(line.split(',')[3])
if add_data[2] in phone:
print('phone已存在')
else:
data = str(int(id_data[-1])+1) + ','+ ','.join(add_data)
new_list.append(data)
f.write(data+' ')
f.close()
return new_list


def syntax_delete(common_data):
# del 语句删除
if len(common_data) == 0:
print('没有该条记录'.center(20,'-'))
return common_data
else:
str_data = ''
f = open(staff_db, 'r+', encoding='utf-8')
for line in f:
if line.split(',')[0] == common_data[0].split(',')[0]:
continue
else:
str_data += line
f.seek(0)
f.truncate()
f.write(str_data)
f.close()
print('已从文件中删除'.center(20, '-'))
return common_data


def syntax_find(con_sen,common_data): #common_data(存储匹配数据) con_sen 例如find * 前两位数据
# find 语句查找
if '*' in con_sen.split('find')[1]: #*号在con_sen
return common_data #返回common_data
sen = con_sen.split('find')[1].split(',')
li = []
for index, i in enumerate(staff_order):
for j in sen:
if j.strip() == i:
li.append(index)
new_data = []
for i in common_data:
str_data = ''
for index, j in enumerate(i.split(',')):
for k in li:
if k == index:
str_data += j + ','
break
new_data.append(str_data[:-1])
return new_data

#common_data(存储匹配数据) #con_sentence =find name,age from staff_table data =age>22
def syntax_where(common_data, con_sentence):
# 查找语句分配
if con_sentence == '': #con_sentence =空
add_data = common_data.split(',') #赋值common_data.split
return syntax_add(add_data) #执行syntax_add(add_data)函数
else:
if 'from' in con_sentence: #如果里面有from 语句
con_sen = con_sentence.split('from') #con_senterce.split用from 分
if con_sen[1].strip() == staff: #如果con_sen[1]字符串里有‘staff’
if 'find' in con_sen[0]: #如果‘find’在con_sen[0]
return syntax_find(con_sen[0],common_data) #执行syntax_find()
elif 'del' in con_sen[0]: #如果del 在common_data
return syntax_delete(common_data)
else:
print('输入的语句有问题')
return []
elif 'set' in con_sentence and 'update' in con_sentence:
con_sen = con_sentence.split('set')
if 'staff_table' in con_sen[0]:
return syntax_update(con_sen[1].split('='), common_data)
else:
print('输入的语句有问题')
return []
else:
print('输入的语句有问题')
return []

#common_op == con_where = [] # 存储区配的数据结果
def common_op(where_data): #common_op(data)
# > < = like 公共方法
list_where_data = [] #赋值从命令行匹配的符号
con_where = [] #存储区配的数据结果
index_num = 0
op = ['>', '<', '=', 'like']
for index, i in enumerate(op): #遍历OP 符号。
if i in where_data: #如果符号 和 where_data匹配上

list_where_data = where_data.split(i) #用符号分,赋值给list_where_data

index_num = index #匹配上后赋值索引值
break #跳出循环
f = open(staff_db, 'r', encoding='utf-8')

for index, i in enumerate(staff_order):
if i == list_where_data[0].strip(): #如果匹配上 age 赋值 I
for line in f: #遍历数据库
data = line.strip().split(',') # 转成列表,赋值data

if index_num == 0: #如果索引为0 对应的大于,
if eval(data[index].strip()) > eval(list_where_data[1].strip()):#
# print("data,listwhere_data",data,list_where_data)

con_where.append(line.strip()) #列表data大于list_where_data,就添加list到con_where
elif index_num == 1: #index_num=1 对应的小于
if eval(data[index].strip()) < eval(list_where_data[1].strip()):
# print("data:,listwhere_data:", eval(data[index].strip()) , eval(list_where_data[1].strip()))
# print('data1,data2,data3,listwhere_data1,listwhere_data2',data[1],data[2],data[3],list_where_data[0],list_where_data[1])
con_where.append(line.strip()) # 添加年龄小于的值到con_where
elif index_num == 2:
if data[index].strip().isdigit(): # 判断原有的内容是否是整数类型
if eval(data[index].strip()) == eval(list_where_data[1].strip()):
con_where.append(line.strip())
else:

if data[index].strip().lower() == eval(list_where_data[1].strip().lower()):
con_where.append(line.strip())
elif index_num == 3:
if data[index].strip().split('-')[0].strip() == eval(list_where_data[1].strip()):
con_where.append(line.strip())

break
f.close()
return con_where


def syntax_parser(cmd):
if 'add' in cmd: #如果add存在
print_search(syntax_where(cmd.split(staff)[1], '')) #执行print_search(syntax_where(cmd.split(staff)[1],))
elif 'where' in cmd: #如果有where
con_sentence, data = cmd.split('where') #con_sentence =find name,age from staff_table data =age>22

print_search(syntax_where(common_op(data), con_sentence))#执行print_search(sumtax_where(common_op(data),find name,age from staff_table)


def main():
while True:
cmd = input('输入语句>>>:').strip().lower()
if not cmd:
continue
syntax_parser(cmd)


if __name__ == '__main__':

main()

原文地址:https://www.cnblogs.com/anzhangjun/p/8465819.html