作业 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. [EXP]Memu Play 6.0.7 - Privilege Escalation

    # Exploit Title: Memu Play - Privilege Escalation (PoC) # Date: // # Author: Alejandra Sánchez # Ven ...

  2. 剑指offer【02】- 替换空格(Java)

    题目:替换空格 考点:字符串 题目描述: 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. ...

  3. Python中的几种矩阵乘法(转)

    一.  np.dot() 1.同线性代数中矩阵乘法的定义.np.dot(A, B)表示: 对二维矩阵,计算真正意义上的矩阵乘积. 对于一维矩阵,计算两者的内积. 2.代码 [code] import ...

  4. Build step 'Execute shell' marked build as failure解决

    今天jenkins构建时运行脚本报错如下: Build step 'Execute shell' marked build as failure 脚本没问题后来看了下原因是磁盘空间不足导致报错,清除下 ...

  5. SignalR 中使用 MessagePack 序列化提高 WebSocket 通信性能

    It's like JSON.but fast and small. MessagePack is an efficient binary serialization format. It lets ...

  6. centos7环境开启WIFI热点

    1.环境介绍 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@localhost ~]# u ...

  7. 学习html5的网站

    http://www.html5cn.org/ http://www.html5china.com/ http://www.mhtml5.com/

  8. SQL Server新建登录名,实现SQL Server身份验证模式

    一.如果是默认安装,没有选择SQL Server和Windows身份验证模式,需要重新设置. 1.右击服务器,属性,常规项中点选“SQL Server和Windows身份验证模式” 2.点击“确定”按 ...

  9. C语言之链表的使用

    C语言链表初学者都说很难,今天就来为大家讲讲链表 讲链表之前不得不介绍一下结构体,在链表学习之前大家都应该已经学了结构体,都知道结构体里面能有许多变量,每个变量可以当做这个结构体的属性,例如: str ...

  10. LinQ是什么?

    •LINQ(发音:Link)是语言级集成查询(Language INtegrated Query) •LINQ是一种用来进行数据访问的编程模型,使得.NET语言可以直接支持数据查询 •LINQ的目标是 ...