作业 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. Xamarin.Android 使用ListView绑定数据

    使用ListView进行数据绑定 第一步:新建一个类,用于存储显示字段. namespace ListViewDataBIndDemo { public class Person { public i ...

  2. Odoo : ORM API

    记录集 model的数据是通过数据集合的形式来使用的,定义在model里的函数执行时它们的self变量也是一个数据集合 class AModel(models.Model): _name = 'a.m ...

  3. Oracle 基本知识回顾

    1.查找数据库所用的字符集编码:SELECT USERENV('language') FROM DUAL;2.将一个表中的字段,插入到这个表:INSERT INTO TABLE SELECT * FR ...

  4. maven多环境参数配置

    maven中properties加载顺序 <build><filters></filters></build>中的配置 pom.xml中的<pro ...

  5. TCP/IP 笔记 - 广播和本地组播

    在之前第二章介绍IP寻址的时候有介绍到,IP地址有4种:单播.组播.广播.任播. 单播,客户端与服务器之间点到点连接通信: 组播,在发送者和多个接收者(如某个特定的分组)之间实现点对多点的连接通信: ...

  6. GAN笔记——理论与实现

    GAN这一概念是由Ian Goodfellow于2014年提出,并迅速成为了非常火热的研究话题,GAN的变种更是有上千种,深度学习先驱之一的Yann LeCun就曾说,"GAN及其变种是数十 ...

  7. Spring Boot初识(4)- Spring Boot整合JWT

    一.本文介绍 上篇文章讲到Spring Boot整合Swagger的时候其实我就在思考关于接口安全的问题了,在这篇文章了我整合了JWT用来保证接口的安全性.我会先简单介绍一下JWT然后在上篇文章的基础 ...

  8. js仿qq分组折叠效果

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. System.Action的使用(lambda 表达式)

    对于Action的使用方法使用如下: namespace ConsoleApp1 { class Program { static void Main(string[] args) { string ...

  10. python 输出颜色与样式的方法

    上次遇到这个问题就想写下来,其实当时我也不怎么会,老师说这个东西不需要理解,只需要死记硬背,写的多了就记住了,所以今天搜集了几篇文章,加上自己的理解,写下了这篇python 输出颜色的样式与方法的文章 ...