作业 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"
查到的信息,打印后,最后面还要显示查到的条数
可创建新员工纪录,以phone做唯一键,staff_id需自增
可删除指定员工信息纪录,输入员工id,即可删除
可修改员工信息,语法如下:
  UPDATE staff_table SET dept="Market" WHERE where dept = "IT"
注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码

详细描述参考http://www.cnblogs.com/alex3714/articles/5740985.html

代码才写了一半,今天 有公开课就没有写完成!先上一部分,明天再战

 #!usr/bin/env python
#-*-coding:utf-8-*-
# Author calmyan
import os ,sys
BASE_DIR=os.path.dirname(os.path.abspath(__file__))#获取相对路径转为绝对路径赋于变量
sys.path.append(BASE_DIR)#增加环境变量 select_='select'#定义关键字
from_='from'
where_='where'
update_='update'
delete_='delete'
insert_='insert'
values_='values'
into_='into'
set_='set' def fj(line):#分解字符串
a=line.split(',')#用 , 分割为列表
dict_list={}#定义一个字典
dict_list[a[3]]={'id':int(a[0]),'name':a[1],'age':int(a[2]),'dept':a[4],'ennoll_date':a[5].strip()}
#print(dict_list)
return dict_list# def dict_info(file_name):#员工信息表提取函数
user_dict={}#定义一个员工信息的字典
with open(file_name,'r',encoding='utf-8') as user_info:
for line in user_info:
user_dict.update(fj(line))#更新员工信息的字典
return user_dict def sql_parsing(sql):#sql解析函数 主入口
#sql=sql.lower()
print(sql)
if sql.startswith('select'):#判断语句类型
_dict_=select_par(sql)#调用相关函数
elif sql.startswith('update') or sql.startswith('UPDATE') :#判断语句类型
_dict_=update_par(sql.lower())
elif sql.startswith('insert'):#判断语句类型
_dict_=insert_par(sql)
elif sql.startswith('delete'):
_dict_=delete_par(sql)
else:
print('输入有误,请重新输入!')
return _dict_#返回解析完成的列表 def select_par(sql):#select语句的解析函数
list_key=parsing(sql,index_select=-1,index_from=-1,index_where=-1,select_=select_,where_=where_,from_=from_)#查询调用
return list_key
def update_par(sql):#update语句的解析函数
list_key=parsing(sql,index_update=-1,index_set=-1,update_=update_,set_=set_,where_=where_)#更新修改调用
return list_key
def insert_par(sql):#insert语句的解析函数
list_key=parsing(sql,index_insert=-1,index_into=-1,index_values=-1,insert_=insert_,values_=values_)#添加调用
return list_key def delete_par(sql):#delete语句的解析函数
list_key=parsing(sql,index_delete=-1,index_from=-1,index_where=-1,delete_=delete_,where_=where_,from_=from_)#删除调用
return list_key def where_format(where_list):#格式化where 子句函数
#print(where_list)
list_format=[]
key_=['and','or','like','not']
str=''#字符连接变量
for i in where_list:
if len(i)==0:continue#如果为空就不连接
if i not in key_:##如果不是关键字就进行连接
str+=i
elif i in key_ and len(str)!=0:
list_format.append(str)#之前的字符串添加到列表
list_format.append(i)#关键 字添加到列表
str=''#清空变量 备用
# if i in key_ and len(str)!=0:#如果循环到关键 字
# list_format.append(str)#之前的字符串添加到列表
# list_format.append(i)#关键 字添加到列表
# str=''#清空变量 备用
# else:#如果不是关键字就进行连接
# str+=i#字符连接
# print(str)
list_format.append(str)#最后的字符串
#print(list_format)
return list_format def select_format(select_list):#格式化select函数
print(select_list)
#str=''
if select_list[0]=='*':#如果为* 就不改动
pass
else:
# for i in select_list:#列表内容转为字符串
# str+=i
list_=str_conn(select_list).split(',')#字符串连接函数
#list_=str.split(',')#
print(list_)
return list_ #字符串连接函数
def str_conn(list):
str=''
for i in list:
str+=i
return str def parsing(sql,**kwargs):#select语句的解析函数
list_l=sql.split(' ')#分割存为列表
index_from=-1#下标定义from
index_select=-1#select
index_where=-1#where
index_update=-1#update
index_set=-1#set
index_delete=-1#delete
index_insert=-1#insert
index_values=-1#valuse
list_key={'select':[],'update':[],'delete':[],'insert':[],'set':[],'from':[],'into':[],'where':[],'values':[]}#定义要处理的关键字字典
for index,i in enumerate(list_l):#获取下标
if i==select_:#如果是关键字select字典下标
index_select=index
if i==update_:#如果是关键字update字典下标,全部转为小写
index_update=index
if i==set_:
index_set=index
if i==insert_:#如果是关键字insert字典下标
index_insert=index
if i==into_:
index_into=index
if i==values_:
index_values=index
if i==delete_:
index_delete=index
if i==from_:#如果是关键字from字典下标
index_from=index
if i==where_:#如果是关键字where字典下标
index_where=index for index,i in enumerate(list_l):#用下标界定,对进行关键字字典的添加
if index_select !=-1 and index>index_select and index<index_from:#在select和from中添加到select中,不为空时
list_key[select_].append(i)#添加当前值
if index_update !=-1 and index==index_update:#如果是update语句添加一个1值
list_key[update_].append(1)
if index_insert !=-1 and index==index_insert:#如果是insert语句添加一个1值
list_key[insert_].append(1)
if index_delete !=-1 and index==index_delete:#如果是delete语句添加一个1值
list_key[delete_].append(1)
if index_from!=-1 and index>index_from and index<index_where:#添加from中的值
list_key[from_].append(i)
if index_set!=-1 and index>index_set and index<index_where:
list_key[set_].append(i)
if index_where!=-1 and index>index_where:
list_key[where_].append(i)
if index_values!=-1 and index>index_values:
list_key[values_].append(i)
if list_key.get(where_):#如果字典在的列表不为空
list_key[where_]=where_format(list_key.get(where_))#进行格式化,重新赋于字典,调用格式化函数
if list_key.get(select_):#如果字典select在的列表不为空
list_key[select_]=select_format(list_key.get(select_))#进行格式化,重新赋于字典
if list_key.get(values_):#如果字典values在的列表不为空
list_key[values_]=select_format(list_key.get(values_))#进行格式化,重新赋于字典
return list_key #执行部分
def sql_perform(sql_dict):#执行函数
print() def perform_select(sql_dict):#执行select操作的函数
select_dict=sql_dict
return select_dict#返回处理后的信息 def perform_update(sql_dict):#执行update操作的函数
select_dict=sql_dict
return select_dict#返回处理后的信息 def perform_insert(sql_dict):#执行insert操作的函数
select_dict=sql_dict
return select_dict#返回处理后的信息 def perform_delete(sql_dict):#执行delete操作的函数
select_dict=sql_dict
return select_dict#返回处理后的信息 print("\033[35;1m欢迎进入员工信息表查询系统\033[0m".center(60,'='))
if __name__ =='__main__':
while True:
print('查询修改示例'.center(50,'-').center(60,' '))
print('''
\033[32;1m1、 select name,age from staff_table where age>22\033[0m
\033[31;1m2、 UPDATE staff_table SET dept="Market" WHERE dept="IT"\033[0m
\033[33;1m3、 insert into staff_table values Alex Li,22,13651054608,IT,2013-04-01\033[0m
\033[32;1m3、 delete from staff_table where id=5 \033[0m
''')
sql=input('sql->').strip()#定义显示提示符
if sql=='exit':break#如果输入exit退出
if sql==0:continue#如何没输入重新提示
sql_dict=sql_parsing(sql)#解析输入的sql语句
print(sql_dict)
sql_action=sql_perform(sql_dict)#执行解析后的语句
file_name='db/staff_table'
#print(dict_info(file_name))#传入要查询的表名
user_info=dict_info(file_name)#传入要查询的表名,添加到字典

python第十五天-原来还差一份作业的更多相关文章

  1. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  2. 孤荷凌寒自学python第二十五天初识python的time模块

    孤荷凌寒自学python第二十五天python的time模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 通过对time模块添加引用,就可以使用python的time模块来进行相关的时间操 ...

  3. 孤荷凌寒自学python第十五天python循环控制语句

    孤荷凌寒自学python第十五天python循环控制语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中只有两种循环控制语句 一.while循环 while 条件判断式 1: ...

  4. 初学 Python(十五)——装饰器

    初学 Python(十五)--装饰器 初学 Python,主要整理一些学习到的知识点,这次是生成器. #-*- coding:utf-8 -*- import functools def curren ...

  5. Python进阶(十五)----面向对象之~继承(单继承,多继承MRO算法)

    Python进阶(十五)----面向对象之~继承 一丶面向对象的三大特性:封装,继承,多态 二丶什么是继承 # 什么是继承 # b 继承 a ,b是a的子类 派生类 , a是b的超类 基类 父类 # ...

  6. 流畅的python第十五章上下文管理器和else块学习记录

    with 语句和上下文管理器for.while 和 try 语句的 else 子句 with 语句会设置一个临时的上下文,交给上下文管理器对象控制,并且负责清理上下文.这么做能避免错误并减少样板代码, ...

  7. python系列十五:Python3 错误和异常

    #!/usr/bin/python #-*-coding:gbk-*- #Python3 错误和异常'''Python 语法错误或者称之为解析错语法分析器指出了出错的一行,并且在最先找到的错误的位置标 ...

  8. selenium python (十五)控制滚动条操作

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #一般用到操作滚动条的两个场景    #注册时的法律条文的阅读,判断用户 ...

  9. Python爬虫(十五)_案例:使用bs4的爬虫

    本章将从Python案例讲起:所使用bs4做一个简单的爬虫案例,更多内容请参考:Python学习指南 案例:使用BeautifulSoup的爬虫 我们已腾讯社招页面来做演示:http://hr.ten ...

随机推荐

  1. 【sping揭秘】21、Spring动态数据源的切换

    对于多个数据源的时候,我们如何切换不同的数据源进行数据库的操作呢? 当然我们可以直接定义2个DataSource,然后在每次获取connection的时候,从不同的DataSource中获取conne ...

  2. Linux学习笔记之七————Linux常用命令之编辑器、服务器

    <1>gedit编辑器 gedit是一个Linux环境下的文本编辑器,类似windows下的写字板程序,在不需要特别复杂的编程环境下,作为基本的文本编辑器比较合适.   <2> ...

  3. jxl 读取xls,并转为二维数组可进行保存

    jxl.jar: 通过java操作excel表格的工具类库 支持Excel 95-2000的所有版本 生成Excel 2000标准格式 支持字体.数字.日期操作 能够修饰单元格属性 支持图像和图表 应 ...

  4. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十六):容器部署项目

    容器部署项目 这一章我们引入docker,采用docker容器的方式部署我们的项目. 首先需要有一个linux环境,并且安装 java 和 maven 以及 docker 环境,这个教程多如牛毛,不再 ...

  5. Facelets应用程序的生命周期

    当客户端(如浏览器)向使用Facelets创建的页面发出新的请求时,会创建一个新的组件树或 javax.faces.component.UIViewRoot将其创建并放入FacesContext. 该 ...

  6. 一文揭秘定时任务调度框架quartz

    之前写过quartz或者引用过quartz的一些文章,有很多人给我发消息问quartz的相关问题, quartz 报错:java.lang.classNotFoundException quartz源 ...

  7. 获取访问者的IP地址

    function getIp() { $realip = NULL; if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ipArray = explode( ...

  8. ASP.NET MVC 5 Authentication Breakdown

    In my previous post, "ASP.NET MVC 5 Authentication Breakdown", I broke down all the parts ...

  9. filezilla通过root账户远程连接管理ubuntu server服务器文件

    前言: 准备重温一下今天在工作中遇见的一个问题,在刚刚安装上的server上测试,做好的文件不是很好传到server项目目录,于是使用了filezilla这个工具,它可以使用ssh来连接,于是乎就引入 ...

  10. C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)

    int [] numbers = new int[5]; // 长度为5,元素类型为 int.string[,] names = new string[5,4]; // 5*4 的二维数组byte[] ...