基础综合知识实现数据库增删改查

  1 # _*_encoding:utf-8_*_
  2 
  3 '''
  4 将员工信息存储到一个文件中,并对文件中的员工信息进行增、删、改、查
  5     员工信息表包括内容:员工编号、姓名、年龄、联系电话、部门、入职日期
  6     文件的第一行为标题:staff_id、name、age、phone、dept、enroll_date
  7 可进行模糊查询,语法至少支持下面3种:
  8   select name,age from staff_table where age > 22
  9   select  * from staff_table where dept = "IT"
 10     select  * from staff_table where enroll_date like "2013"
 11 查到的信息,打印后,最后面还要显示查到的条数
 12 可创建新员工纪录,以staff_id做唯一键,staff_id需自增
 13 可删除指定员工信息纪录,输入员工id,即可删除
 14 可修改员工信息,语法如下:
 15   UPDATE staff_table SET dept="Market" WHERE where dept = "IT"
 16 '''
 17 
 18 def addEmp(fileName, args):
 19     paramStr,paramList,empList = fileRead(fileName)  # 读取字段名称列表和字段值列表
 20     if(len(empList)>0):
 21         maxStaffId = int(empList[-1].split()[0]) + 1
 22     else:
 23         maxStaffId = 1
 24     empStr = "\n" + str(maxStaffId)
 25     paramList = paramList[1:]
 26     for s in paramList:
 27         empStr += " " + args[s]
 28 
 29     fileAppend(fileName, empStr)
 30 
 31 def delEmp(fileName, staffId):
 32     paramStr, paramList, empList = fileRead(fileName)  # 读取字段名称列表和字段值列表
 33     recordList = [paramStr]
 34     for record in empList:
 35         if (int(record.split()[0]) == staffId):
 36             continue
 37         recordList.append(record)
 38     fileWrite(fileName, recordList)
 39 
 40 
 41 def updateEmp(fileName, args, val):
 42     paramStr, paramList, empList = fileRead(fileName)  # 读取字段名称列表和字段值列表
 43 
 44     updateList = findEmp(fileName, args,False)  # 待修改的记录
 45     updateStaffIds = []
 46     for record in updateList:  # 获取待修改记录的staffId列表
 47         updateStaffIds.append(record.split()[0])
 48 
 49     param = val.split()[0]          #需要修改的字段
 50     value = val.split()[1]          #需要修改的值
 51     newList = [paramStr]
 52 
 53     paramIndx = paramList.index(param)  # 根据参数名找到需要修改的值对应的位置,并修改值
 54 
 55     for emp in empList:  # 逐条记录取出来判断当前记录的编号在待修改编号列表内则修改
 56 
 57         if (emp.split()[0] in updateStaffIds):
 58             #将字符串转换成List,将List中的值更新
 59             empls = emp.split()
 60             empls[paramIndx] = value
 61             #再将List转换成字符串,将头尾的“[]”去掉,将List分隔符和字符串分隔符去掉
 62             emp = str(empls)[1:-1].replace(",", "").replace("'", "")+"\n"
 63 
 64         newList.append(emp)
 65     fileWrite(fileName,newList)
 66 
 67 def findEmp(fileName, args,isPrint):
 68     paramStr, paramList, empList = fileRead(fileName)  # 读取字段名称列表和字段值列表
 69 
 70     recordsLen = len(empList)  # 记录个数
 71     if recordsLen > 0:
 72         param = args.split()[0]  # 参数名称
 73         equType = args.split()[1]  # 参数运算符
 74         value = args.split()[2]  # 参数值
 75 
 76         while (recordsLen > 0):  # 取出每条记录进行判断,不符合的记录删除
 77             recordsLen -= 1
 78             paramIndex = paramList.index(param)  # 找到条件参数名称在参数列表中的位置
 79             if (equType == '<'):  # 将列表中对应列值大于的都删除
 80                 if (empList[recordsLen].split()[paramIndex] >= value):
 81                     del empList[recordsLen]
 82             elif (equType == '>'):  # 将列表中对应列值小于的都删除
 83                 if (empList[recordsLen].split()[paramIndex] <= value):
 84                     del empList[recordsLen]
 85             elif (equType == '='):  # 将列表中对应列值不等于的都删除
 86                 if (empList[recordsLen].split()[paramIndex] != value):
 87                     del empList[recordsLen]
 88             elif (equType == 'like'):  # 将列表中对应列值不包含的都删除
 89                 if (empList[recordsLen].split()[paramIndex].find(value) == -1):
 90                     del empList[recordsLen]
 91 
 92     if(isPrint):
 93         for str in empList:
 94             print(str)
 95         print("总共查找到:", len(empList), "条记录!")
 96 
 97     return empList
 98 
 99 #读取指定文件中的所有记录,并将字段名称字符串(paramStr),字段名称组织的列表(paramList),记录值列表(empList)返回
100 def fileRead(fileName):
101     with open(fileName, "r") as fr:
102         paramStr = fr.readline()
103         paramList = paramStr.split()
104         empList = fr.readlines()
105     return paramStr, paramList, empList
106 
107 #将多个记录组成的列表(recordList)数据写入到指定的文件(fileName)中
108 def fileWrite(fileName, recordList):
109     with open(fileName, "w") as fw:
110         for record in recordList:
111             fw.write(record)
112 
113 #将一条字符串格式记录(recordStr)添加到指定的文件(fileName)最后一行
114 def fileAppend(fileName, recordStr):
115     with open(fileName, "a") as fa:
116         fa.write(recordStr)
117 
118 
119 # findEmp("empTable.txt","staff_id > 0",True)
120 
121 empInfo = {"name": "Hong", "age": "2", "phone": "138094", "dept": "Sale", "enroll_date": "2016-03-23"}
122 
123 #addEmp("./datas/empTable.txt",empInfo)
124 
125 #delEmp("./datas/empTable.txt",11)
126 
127 updateEmp("./datas/empTable.txt","staff_id < 5","age 5")

 

posted on 2018-02-09 17:36  星际无垠  阅读(88)  评论(0)    收藏  举报