# _*_ coding:utf-8 _*_import osdef select(data): """ 查询语法一: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" """ data1 = data.split()#传入变量去空格成字符串 if data == ('select name,age from staff_table where age > %s' %(data1[7])): with open("aaa.txt", encoding="utf-8") as f: list = [] for line in f: i = line.strip().split(",") a = i[1] b = i[2] c = [a, b] if b > data1[7]: list.append(c) for i in list: print(','.join(i)) print("查询到%s条符合的信息" %len(list)) elif data == ('select * from staff_table where dept = %s' %(data1[7])): with open("aaa.txt", encoding="utf-8") as f2: list2 = [] for line in f2: i = line.strip().split(",") if data1[7]==i[4]: list2.append(i) for i in list2: print(','.join(i)) print("查询到%s条符合的信息" %len(list2)) elif data == ('select * from staff_table where enroll_date like %s' % (data1[8])): with open("aaa.txt", encoding="utf-8") as f3: list3 = [] for line in f3: i = line.strip().split(",") if data1[8].strip('""') == i[5][:4]: list3.append(i) for i in list3: print(','.join(i)) print("查询到%s条符合的信息" %len(list3))def add(data): """ 添加语法: name,age,phone,dept,enroll-date (例如:张,22,123456789,IT,2017-06-18) """ data1 = data.split(",") with open("aaa.txt", encoding="utf-8")as f: list = [] phone_list = [] for line in f: i = line.strip().split(",") phone_list.append(i[3]) if data1[2] in phone_list: print("phone已存在") else: with open("aaa.txt", "r+", encoding="utf-8")as f: for line in f: i = line.strip().split(",") # print(i) list.append(i) w = str(int(list[-1][0]) + 1) data1.insert(0, w) data1 = ','.join(data1) f.write('\n'+data1) print(data1 +'\n' "添加成功!!!")def remove(data): """ 删除语法:delete from staff_table where staff_id = 12 """ data1 = data.split(" ") if data == ("delete from staff_table where staff_id = %s" %data1[6]): with open("aaa.txt", encoding="utf-8") as f: for line in f: i = line.strip().split(",") if data1[6] == i[0]: i2 = ','.join(i) print(i2) with open("aaa.txt", encoding="utf-8")as f_read,open("aaa_back.txt", "a+", encoding="utf-8")as f_write: for i in f_read: if i2 in i: i = i.replace(i2, "").strip() f_write.write(i) # f_write.flush() os.remove("aaa.txt") os.rename("aaa_back.txt","aaa.txt") print("删除成功!!!")def change(data): """ 修改语法:UPDATE staff_table SET dept="Market" WHERE where dept = "IT" """ data1 = data.split() with open("aaa.txt", encoding="utf-8") as f_read,open("aaa_back.txt","w",encoding="utf-8")as f_write: #f_read读,f_write用来复用 for line in f_read: if data1[8][1:-1] in line:#查看表中的职位是不是IT line = line.replace(data1[8][1:-1],data1[3][6:-1]) f_write.write(line) # f_write.flush() os.remove("aaa.txt") os.rename("aaa_back.txt","aaa.txt") print("修改成功!!!")if __name__ == "__main__": msg = """ 1:查询 2:添加 3:删除 4:修改 5:退出 """ msg_dict = { "1": select, "2": add, "3": remove, "4": change, "5": exit, } while True: print(msg) choice = input("输入序号>>:") if len(choice) == 0 or choice not in msg_dict: continue if choice =='5':break data = input("请输入数据>>:").strip() msg_dict[choice](data)
文件
1,zhang,22,11111111111,Market,2013-04-01 2,wang,30,22222222222,HR,2015-05-03 3,li,25,33333333333,SALES,2016-04-22 4,zhao,40,44444444444,IT,2013-04-01 5,张,22,123456789,Market,2017-06-18 9,张,22,223456789,Market,2017-06-18
staff_id name age phone dept enroll_date
要求
员工信息表程序,实现增删改查操作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.查到的信息,打印后,最后面还要显示查到的条数 3.可创建新员工纪录,以phone做唯一键,staff_id需自增4.可删除指定员工信息纪录,输入员工id,即可删除5.可修改员工信息,语法如下: UPDATE staff_table SET dept="Market" WHERE where dept = "IT"