学生信息管理系统脚本


要求:


1、可进行模糊查询,语法至少支持下面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"
查到的信息,打印后,最后面还要显示查到的条数 
2、可创建新员工纪录,以phone做唯一键,staff_id需自增
3、可删除指定员工信息纪录,输入员工id,即可删除
4、可修改员工信息,语法如下:
  UPDATE staff_table SET dept="Market" WHERE where dept = "IT"


#
_*_coding:utf-8_*_ import os def select(data): #查询一:select name,age from staff_table where age > 数字 #查询二:select * from staff_table where dept = xx #查询三:select * from staff_table where enroll_date like "2015" data1 = data.split(" ") directory = ["staff_id", "name", "age", "phone", "dept", "enroll-date"] if data == ("select name,age from staff_table where age > %s" %(data1[7])): f = open("staff_table") count = 0 for line in f: i = line.strip().split(",") if i[2] > data1[7]: count += 1 print(i) f.close() print("查询到 %s 条符合的信息" %(count)) if data == ('select * from staff_table where enroll_date like %s'%(data1[7])): f = open("staff_table") count = 0 for line in f: i = line.strip().split(",") a = data1[7][1:5] if a in i[5]: print(i) count += 1 print("查询到 %s 条符合的信息" %(count)) else: f = open("staff_table") list = [] for line in f: i = line.strip().split(",") if data == ("select * from staff_table where %s = %s" %(data1[5], i[(directory.index(data1[5]))])): for m in i: if m == i[directory.index(data1[5])]: list.append(i) for h in list: print(h) print("查询到 %s 条符合的信息" % len(list)) def add(data): #添加语法: name,age,phone,dept,enroll-date data1 = data.split(",") f= open("staff_table") list = [] phone_list = [] for line in f: i = line.strip().split(",") q = i[3] phone_list.append(q) if data1[2] in phone_list: print("手机号已经存在") f.close() else: f1 = open("staff_table", "r+") for line in f1: lines = line.strip().split(",") list.append(lines) i = line.strip().split(",") w = str(int(len(list)) + 1) data1.insert(0, w) print(data1) data1 = ','.join(data1) f1.write("\n") f1.write(data1) f1.close() print("添加成功!!!") def remove(data): #删除语法:delete from staff_table where staff_id = xxx data1 = data.split(" ") if data == ("delete from staff_table where staff_id = %s" %data1[6]): with open("staff_table") as f: for line in f: i = line.strip().split(",") i1 = line.splitlines() if data1[6] == i[0]: i2 = ','.join(i1) print(i2) a = i2 f = open("staff_table") f1 = open("staff_table_1", "a+") for i in f: if a in i: i = i.replace(a, "").strip() f1.write(i) f1.flush() f.close() f1.close() os.remove("staff_table") os.rename("staff_table_1", "staff_table") print("删除成功!!!") def change(data): #修改语法:UPDATE staff_table SET dept = IT where dept = xxx data1 = data.split(" ") with open("staff_table") as f,\ open("staff_table1","w")as f1: for line in f: lines = line.strip() if data1[5] in lines: lines = lines.replace(data1[5],data1[9]) print(lines) f1.write(lines) f1.write("\n") f1.flush() os.remove("staff_table") os.rename("staff_table1","staff_table") print("修改成功!!!") msg_list =["select","add","remove","change","exit"] while True: print("欢迎光临学生信息查询系统") for i in msg_list: print(i) choice = input("你的操作") if len(choice) == 0 or choice not in msg_list: continue if choice == 'exit': break if choice == "select" : while True: data = input("请输入数据>>:").strip() if len(data) == 0: continue select(data) if choice == "add" : while True: data = input("请输入数据>>:").strip() if len(data) == 0: continue add(data) if choice == "remove" : while True: data = input("请输入数据>>:").strip() if len(data) == 0: continue remove(data) if choice == "change" : while True: data = input("请输入数据>>:").strip() if len(data) == 0: continue change(data)

 

posted @ 2017-02-09 21:35  suhanlei  阅读(51)  评论(0)    收藏  举报