学员信息表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学员信息表相关的知识,希望对你有一定的参考价值。
作业:
有以下员工信息表
当然此表你在文件存储时可以这样表示
1
|
1 ,Alex Li, 22 , 13651054608 ,IT, 2013 - 04 - 01 |
现需要对这个员工信息文件,实现增删改查操作
- 可进行模糊查询,语法至少支持下面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 where dept = "IT"
注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!
下面是代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#user:Felix
#date:2016-11-1
#mail:[email protected]
#If you have questions or suggestions, please contact the author.
import os
import time
import re
import shutil
def data(data):
data=data.strip().replace(","," ").split(" ")
# if data[0] == "select":
# print("select")
# elif data[0] == "update":
# print("update")
# elif data[0] == "insert":
# print("insert")
# elif data[0] == "delete":
# print("delete")
# else:
# print("输入语法有问题")
tag=False
for i in student_list[0]:
if i in data:
column.append(i)
for i in data:
if i == "where":
tag=True
continue
if tag:
condition.append(i)
return column,condition
def select(data):
select_list=[]
data=data.replace("select","").replace("from","").replace("staff_table where","")
#print(data) # * age <= 22
mark=student_list[0].index(if_information[1][0]) #select name,age from staff_table where occupation like I
for i in student_list:
if i == student_list[0]:continue #如果是列名就跳出
#print("i[mark] ",i[mark],"if_information[1][2] ",if_information[1][2],"***",type(i[mark])," ",type(if_information[1][2]))
#if i[mark].isdigit():i[mark]=int(i[mark])
#if if_information[1][2].isdigit():if_information[1][2]=int(if_information[1][2])
if if_information[1][2].isdigit():
if if_information[1][1] == ">":
if int(i[mark]) > int(if_information[1][2]):select_list.append(i)#print(i)
elif if_information[1][1] == "=":
if int(i[mark]) == int(if_information[1][2]):select_list.append(i)#print(i)
elif if_information[1][1] == "<":
if int(i[mark]) < int(if_information[1][2]):select_list.append(i)#print(i)
elif if_information[1][1] == ">=":
if int(i[mark]) >= int(if_information[1][2]):select_list.append(i)#print(i)
elif if_information[1][1] == "<=":
if int(i[mark]) <= int(if_information[1][2]):select_list.append(i)#print(i)
elif if_information[1][1] == "!=":
if int(i[mark]) != int(if_information[1][2]):select_list.append(i)#print(i)
elif if_information[1][1] == "like":
if if_information[1][2] in i[mark]:select_list.append(i)#print(i)
else:print("您的输入有错")
#以上是 age和id int类型查询
else:
#print(i[mark],if_information[1][1],if_information[1][2])
if if_information[1][1] == "=":
if i[mark] == if_information[1][2]:select_list.append(i)#print(i)
elif if_information[1][1] == "like":
if if_information[1][2] in i[mark]:select_list.append(i)#print(i)
else:print("您的输入有错")
#以上是字符串类型查询
print_selcet_list=if_information[0]
#print(print_selcet_list,if_information[0],student_list[0])
count=[]
if "*" in data:
print(student_list[0])
for line in select_list:
print(line)
print("您一空查询到\\033[1;32;31m%s\\033[0m条信息!"%(len(select_list)))
else:
for i in print_selcet_list:
mark=student_list[0].index(i)
count.append(int(mark))
#print(count)
print(print_selcet_list)
for line in select_list:
if len(count) == 0:print(line[count[0]],line[count[1]],line[count[2]],line[count[3]],line[count[4]],line[count[5]])
elif len(count) == 1:print(line[count[0]])
elif len(count) == 2:print(line[count[0]],line[count[1]])
elif len(count) == 3:print(line[count[0]],line[count[1]],line[count[2]])
elif len(count) == 4:print(line[count[0]],line[count[1]],line[count[2]],line[count[3]])
elif len(count) == 5:print(line[count[0]],line[count[1]],line[count[2]],line[count[3]],line[count[4]])
elif len(count) == 6:print(line[count[0]],line[count[1]],line[count[2]],line[count[3]],line[count[4]],line[count[5]])
print("您一空查询到\\033[1;32;31m%s\\033[0m条信息!"%(len(select_list)))
# for line in select_list:
# for i in count:
#
# print(line[int(i)])
return select_list
def update(data_update):
#UPDATE staff_table SET dept="Market" WHERE dept = "IT"
#update staff_table set dept=market where dept = it
# print(data_update)
# print(if_information)
data1=data(data_update)[1]
news_ids = []
for id in data1:
if id not in news_ids:
news_ids.append(id)
news_ids.insert(0,"*")
# print(type(news_ids))
# print(news_ids)
select_list=select("".join(news_ids))
awk=data_update.strip().replace(","," ").split(" ")[3:6]
select_list_id=student_list[0].index(awk[0])
with open("dbnew","w",encoding="utf-8") as write_file:
#print(",".join(student_list[0]))
print(student_list[0])
write_file.write(",".join(student_list[0])+"\\n")
for i in range(len(select_list)):
student_list.remove(select_list[i])
select_list[i][select_list_id]=awk[2]
print(select_list[i])
string=",".join(select_list[i])
#print(string)
write_file.write(string+"\\n")
for s in student_list:
if "staff_id" in s:continue
#print(s)
else:write_file.write(",".join(s)+"\\n")
print("您已经修改\\033[1;32;31m%s\\033[0m条信息!"%(len(select_list)))
#现在已经查出来了 写入新文件并同时修改
#print(awk)
write_file.close()
dbbak="dbbak"+time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time()))
shutil.copyfile("db",dbbak)
os.remove("db")
os.renames("dbnew","db")
# with open("db","r",encoding="utf-8") as read_file,\\
# open("dbnew","w",encoding="utf-8") as write_file:
# for r_line in read_file:
# for w_line in write_file:
# if
#for line in student_list:
#line
def insert(data):
data = re.split(‘\\(|\\)‘,data)[-2].replace(" ","")
data1= re.split(‘\\(|\\)‘,data)
#print(data1)
for line in student_list:
if line[3] in data:
print("新增用户手机号与老用户\\033[1;32;31m%s\\033[0m有冲突"%line)
break
else:
sum=[]
with open("db","r",encoding="utf-8") as read_file,\\
open("dbnew","w",encoding="utf-8") as write_file:
for r_line in read_file:
if "staff_id" not in r_line:sum.append(r_line.split(",")[0])
write_file.write(r_line)
#print(type(",".join(data)))
# print(str(int(sum[-1])+1))
id=int(sum[-1])+1
# print(type(id))
# print(type(repr(id)))
#write_file.write("\\n"+repr(int(sum[-1])+1)+","+",".join(data))
new_line="\\n"+repr(id)+","+data
write_file.write(new_line)
# read_file.close()
# write_file.close()
dbbak="dbbak"+time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time()))
shutil.copyfile("db",dbbak)
os.remove("db")
os.renames("dbnew","db")
def delete(data_del):
#DELETE FROM staff_table WHERE staff_id = 1
print(data_del)
delete=data(data_del)[1][3:6]
delete.insert(0,"*")
print(delete)
select_list=select("".join(delete))
awk=data(data_del)[1][3:6]
print(awk)
select_list_id=student_list[0].index(awk[0])
with open("dbnew","w",encoding="utf-8") as write_file:
#print(",".join(student_list[0]))
print(student_list[0])
write_file.write(",".join(student_list[0])+"\\n")
for i in range(len(select_list)):
student_list.remove(select_list[i])
# select_list[i][select_list_id]=awk[2]
print(select_list[i])
# string=",".join(select_list[i])
# #print(string)
# write_file.write(string+"\\n")
for s in student_list:
if "staff_id" in s:continue
#print(s)
else:write_file.write(",".join(s)+"\\n")
print("您已经删除\\033[1;32;31m%s\\033[0m条信息!"%(len(select_list)))
write_file.close()
dbbak="dbbak"+time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time()))
shutil.copyfile("db",dbbak)
os.remove("db")
os.renames("dbnew","db")
if __name__ == "__main__":
student_list=[]
f=open("db","r",encoding="utf-8")
for line in f:
line=line.strip().split(",")
student_list.append(line)
#print(student_list)
# msg=‘‘‘
# 1:查询
# 2:添加
# 3:删除
# 4:修改
# 5:退出
# ‘‘‘
# menu_dic={
# ‘1‘:select,
# ‘2‘:insert,
# ‘3‘:delete,
# ‘4‘:update,
# ‘5‘:"",
# }
while True:
column=[]
condition=[]
#print(msg)
choice=input("数据>>: ").strip().lower().replace("\\"","").replace("\\‘","").replace(";","")
#choice="""INSERT INTO `staff_table`.`staff` VALUES (‘刘耀‘, ‘20‘, ‘17777777777‘,‘运维‘,"2011-11-11");"""
#choice=choice.strip().lower().replace("\\"","").replace("\\‘","").replace(";","")
#print(choice)
str=choice
if_information=list(data(str)) #[[‘name‘, ‘age‘], [‘age‘, ‘>‘, ‘22‘]]
#print("if_information==",if_information)
choice_def=choice.strip().replace(","," ").split(" ")
f.close()
if choice_def[0] == "select":
select(str)
elif choice_def[0] == "update":
update(str)
elif choice_def[0] == "insert":
insert(str)
elif choice_def[0] == "delete":
delete(str)
else:
print("输入语法有问题")
# if len(choice) == 0 or choice not in menu_dic:continue
# if choice == "5":break
#str=input("数据>>: ").strip()
# str="select name,age from staff_table where age > 22"
# if_information=list(data(str))
# menu_dic[choice](str)
#print(if_information)
# select name,age from staff_table where age > 22
# select name,age from staff_table where dept like I
以上是关于学员信息表的主要内容,如果未能解决你的问题,请参考以下文章