首页 技术 正文
技术 2022年11月22日
0 收藏 374 点赞 4,771 浏览 5793 个字

作业 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

python第十五天-原来还差一份作业

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

 #!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模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850