需求:

1.可进行模糊查询,语法至少支持下面3种:

1.select name,age from staff_table where age > 22

2.select  * from staff_table where dept = IT

3.查到的信息,打印后,最后面还要显示查到的条数

2.可创建新员工纪录,以phone做唯一键,staff_id需自增

3.可删除指定员工信息纪录,输入员工id,即可删除

4.可修改员工信息,语法如下:

1.UPDATE staff_table SET dept= 无 where dept = IT

注意:请输入正确的语句进行查询,否则会报错。

  1. #_*_coding:utf-8_*_
  2. import os
  3. def fetch(data):
  4. #查询语法一:select name,age from staff_table where age > 22
  5. #查询语法二:select * from staff_table where dept = IT
  6. data1 = data.split(" ")
  7. directory = ["staff_id", "name", "age", "phone", "dept", "enroll-date"]
  8. if data == ("select name,age from staff_table where age > %s" %(data1[7])):
  9. with open("xinxi", encoding="utf-8") as f:
  10. list = []
  11. list1 = []
  12. list2 = []
  13. for line in f:
  14. i = line.strip().split(",")
  15. w = i[1]
  16. e = i[2]
  17. a = [w, e]
  18. if e > data1[7]:
  19. list2.append(a)
  20. for i in list2:
  21. print(i)
  22. print("查询到 %s 条符合的信息" %len(list2))
  23. else:
  24. with open("xinxi", encoding="utf-8") as f:
  25. list = []
  26. for line in f:
  27. i = line.strip().split(",")
  28. q = i[0]
  29. w = i[1]
  30. e = i[2]
  31. r = i[3]
  32. t = i[4]
  33. y = i[5]
  34. if data == ("select * from staff_table where %s = %s" % (data1[5], i[(directory.index(data1[5]))])):
  35. list.append(i)
  36. else:
  37. continue
  38. for j in list:
  39. print('.'.join(j))
  40. print("查询到 %s 条符合的信息" %len(list))
  41. return 0
  42. def add(data):
  43. #添加语法: name,age,phone,dept,enroll-date (例如:王新凯,22,11111111111,IT,2015-10-31)
  44. data1 = data.split(",")
  45. f= open("xinxi",encoding="utf-8")
  46. all_list = []
  47. list = []
  48. phone_list = []
  49. for line in f:
  50. i = line.strip().split(",")
  51. q = i[3]
  52. phone_list.append(q)
  53.  
  54. if data1[2] in phone_list:
  55. print("手机号已存在")
  56. f.close()
  57. else:
  58. f1 = open("xinxi", "r+", encoding="utf-8")
  59. for line in f1:
  60. lines = line.strip().split(",")
  61. # print(lines)
  62. list.append(lines)
  63. i = line.strip().split(",")
  64. w = str(int(list[-1][0]) + 1)
  65. data1.insert(0, w)
  66. print(data1)
  67. data1 = ','.join(data1)
  68. f1.write("\n")
  69. f1.write(data1)
  70. f1.close()
  71. print("添加成功!!!")
  72. def remove(data):
  73. #删除语法:delete from staff_table where staff_id = 12
  74. data1 = data.split(" ")
  75. if data == ("delete from staff_table where staff_id = %s" %data1[6]):
  76. with open("xinxi", encoding="utf-8") as f:
  77. list = []
  78. for line in f:
  79. i = line.strip().split(",")
  80. i1 = line.splitlines()
  81. q = i[0]
  82. if data1[6] == q:
  83. i2 = ','.join(i1)
  84. print(i2)
  85. list.append(i)
  86. a = i2
  87. f = open("xinxi", encoding="utf-8")
  88. f1 = open("back", "a+", encoding="utf-8")
  89. for i in f:
  90. if a in i:
  91. i = i.replace(a, "").strip()
  92. f1.write(i)
  93. f1.flush()
  94. f.close()
  95. f1.close()
  96. os.remove("xinxi")
  97. os.rename("back","xinxi")
  98. print("删除成功!!!")
  99. return
  100. def change(data):
  101. #修改请输入(注意空格和没有引号):UPDATE staff_table SET dept = IT where dept = 运维
  102. data1 = data.split(" ")
  103. print(data1)
  104. # a =','.join(data1[3])
  105. # print(a)
  106. directory = ["staff_id", "name", "age", "phone", "dept", "enroll-date"]
  107. var = int(directory.index(data1[3]))
  108. with open("xinxi", encoding="utf-8") as f,\
  109. open("back","w",encoding="utf-8")as f1:
  110. for line in f:
  111. lines = line.strip()
  112. print(lines)
  113. if data1[5] in lines:
  114. lines = lines.replace(data1[5],data1[9])
  115. f1.write(lines)
  116. f1.write("\n")
  117. f1.flush()
  118. os.remove("xinxi")
  119. os.rename("back","xinxi")
  120. print("修改成功!!!")
  121.  
  122. if __name__ == "__main__":
  123. msg = """
  124. 1:查询
  125. 2:添加
  126. 3:删除
  127. 4:修改
  128. 5:退出
  129. """
  130. msg_dict = {
  131. "": fetch,
  132. "": add,
  133. "": remove,
  134. "": change,
  135. "": exit,
  136. }
  137. while True:
  138. print(msg)
  139. choice = input("输入序号>>:")
  140. if len(choice) == 0 or choice not in msg_dict: continue
  141. if choice =='':break
  142. data = input("请输入数据>>:").strip()
  143. msg_dict[choice](data)

Python3.5 day4作业:对员工信息文件,实现增删改查操作。的更多相关文章

  1. C#+Access 员工信息管理--简单的增删改查操作和.ini配置文件的读写操作。

    1.本程序的使用的语言是C#,数据库是Access2003.主要是对员工信息进行简单的增删改查操作和对.ini配置文件的读写操作. 2.代码运行效果如下: 功能比较简单.其中在得到查询结果后,在查询结 ...

  2. Java使用DOM4J对XML文件进行增删改查操作

    Java进行XML文件操作,代码如下: package com.founder.mrp.util; import java.io.File; import java.util.ArrayList; i ...

  3. python文件实现增删改查操作

    # coding = utf-8 import os import json import re ''' 本程序旨在将练习基础知识部分,包括: 列表,元组,字典,文件,函数,字符串等知识 实现的功能: ...

  4. 老男孩Day4作业:员工信息查询系统

    1.作业需求: (1).工信息表程序,实现增删改查操作: (2).可进行模糊查询,语法至少支持下面3种:          select name,age from staff_table where ...

  5. MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存

    二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...

  6. MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存

    目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...

  7. 【练习】Python第四次:实现对文件的增删改查

    一,实现对文件的增删改查 (一),三级菜单的处理结构及退出技巧:使用TAG标记 tag=True while tag: print('leve1') choice=input("level1 ...

  8. 基于SpringMVC的文件(增删改查)上传、下载、更新、删除

    一.项目背景 摘要:最近一直在忙着项目的事,3个项目过去了,发现有一个共同的业务,那就是附件的处理,附件包括各种文档,当然还有图片等特殊文件,由于时间的关系,每次都是匆匆忙忙的搞定上线,称这项目的空档 ...

  9. 用dom4j解析xml文件并执行增删改查操作

    转自:https://www.aliyun.com/jiaocheng/1339446.html xml文件: <?xml version="1.0" encoding=&q ...

随机推荐

  1. Repeater、地址栏传值、Response--2016年12月30日

    Repeater  Repeater支持以下5种模板       ● ItemTemplate : 对每一个数据项进行格式设置 [Formats each item from the data sou ...

  2. Ubuntu14.04更新源、安装chrome/搜狗输入法

    目录: 1.更新源 2.安装chrome 3.安装搜狗输入法     1.更新源 三步: cp /etc/apt/sources.list /etc/apt/sources.list_backup   ...

  3. appium 滑动

    前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html   知乎Android客户端登陆:htt ...

  4. DIR 按文件名中数字大小进行排序

    @echo off set arg=%1 if "%arg%" == "" set arg=* if "%arg%" == "-h ...

  5. 创建Odoo8数据库时的“new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)“问题

    Odoo8创建数据库时,显示如下错误信息: DataError: new encoding (UTF8) is incompatible with the encoding of the templa ...

  6. 文件上传大小js判断

    function fileChange(target) { var fileSize = 0; if (isIE && !target.files) { var filePath = ...

  7. javaScript判断浏览器类型

    <script type="text/javascript"> function getBrowserInfo(){ var OsObject=navigator.us ...

  8. “div+css”下拉菜单

    <style> html, body { margin: 0; padding: 0; } .btn-group{ font-size: 14px; position: relative; ...

  9. sql字段属性

  10. 初识node.js

    Node.js不是一种语言:不是框架:也不是工具.它是用于运行基于JavaScript应用程序的运行时环境.