python第十五天-原来还差一份作业
作业 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第十五天-原来还差一份作业的更多相关文章
- Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块
Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fab ...
- 孤荷凌寒自学python第二十五天初识python的time模块
孤荷凌寒自学python第二十五天python的time模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 通过对time模块添加引用,就可以使用python的time模块来进行相关的时间操 ...
- 孤荷凌寒自学python第十五天python循环控制语句
孤荷凌寒自学python第十五天python循环控制语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中只有两种循环控制语句 一.while循环 while 条件判断式 1: ...
- 初学 Python(十五)——装饰器
初学 Python(十五)--装饰器 初学 Python,主要整理一些学习到的知识点,这次是生成器. #-*- coding:utf-8 -*- import functools def curren ...
- Python进阶(十五)----面向对象之~继承(单继承,多继承MRO算法)
Python进阶(十五)----面向对象之~继承 一丶面向对象的三大特性:封装,继承,多态 二丶什么是继承 # 什么是继承 # b 继承 a ,b是a的子类 派生类 , a是b的超类 基类 父类 # ...
- 流畅的python第十五章上下文管理器和else块学习记录
with 语句和上下文管理器for.while 和 try 语句的 else 子句 with 语句会设置一个临时的上下文,交给上下文管理器对象控制,并且负责清理上下文.这么做能避免错误并减少样板代码, ...
- python系列十五:Python3 错误和异常
#!/usr/bin/python #-*-coding:gbk-*- #Python3 错误和异常'''Python 语法错误或者称之为解析错语法分析器指出了出错的一行,并且在最先找到的错误的位置标 ...
- selenium python (十五)控制滚动条操作
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #一般用到操作滚动条的两个场景 #注册时的法律条文的阅读,判断用户 ...
- Python爬虫(十五)_案例:使用bs4的爬虫
本章将从Python案例讲起:所使用bs4做一个简单的爬虫案例,更多内容请参考:Python学习指南 案例:使用BeautifulSoup的爬虫 我们已腾讯社招页面来做演示:http://hr.ten ...
随机推荐
- 线程安全-002-多个线程多把锁&类锁
一.多个对象多把锁 例子代码: package com.lhy.thread01; public class MultiThread { //static private int num = 0; / ...
- 新电脑一般javaweb配置
下个jdk (官网)1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:C:\Program Files\Jav ...
- mysql遇见contains nonaggregated column 'information_schema.PROFILING.SEQ'异常
报错如下:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggrega ...
- tensorflow实现循环神经网络
包括卷积神经网络(CNN)在内的各种前馈神经网络模型, 其一次前馈过程的输出只与当前输入有关与历史输入无关. 递归神经网络(Recurrent Neural Network, RNN)充分挖掘了序列数 ...
- js a标签 + ajax 多参数穿参
<span onclick="return haoping('{$row['jv_id']}','1')"> function haoping(id,type){ $. ...
- [POI 2009]Lyz
Description 题库链接 初始时滑冰俱乐部有 \(1\) 到 \(n\) 号的溜冰鞋各 \(k\) 双.已知 \(x\) 号脚的人可以穿 \(x\) 到 \(x+d\) 的溜冰鞋.有 \(m\ ...
- 【手记】VSTO部署中的坑
局域网部署,客户机安装时报[部署清单签名的证书或其位置不受信任]: 在[Internet 属性]里(可运行inetcpl.cpl打开),把服务器内网IP加进[受信任站点]就好,不用管excel信任中心 ...
- angularjs学习第六天笔记(指令简介学习)
您好,由于周末有事情,没哟学习angularjs,几天晚上开始继续学习angularjs,坚持加油每一天.谢谢 接着上周五学习了表单验证以后,今天开始学习angularjs中一个非常重要的模块:指令 ...
- Java基础——Oracle(一)
Oracle是目前最流行的数据库之一.功能强大,性能卓越.所以学起来比较困难.学习Oracle需要具备一定的基础.比如学习过一门编程语言,或者学过其他的数据库等,没有一些基础很难下手. 一.Oracl ...
- C# 填充Excel
1.添加引用 Microsoft.Office.Interop.Excel; 2.使用命名空间 using Microsoft.Office.Interop.Excel; 3.填充EXCEL单元格方法 ...