Python编程 模拟SQL语句 实现对员工信息的增删改查
2023-04-18 13:09:27 时间
一、问题描述
用 Python 模拟 sql 语句,实现对员工信息的增删改查。
- 封装函数,传入参数:文件路径和 sql 命令。
- 模拟 sql 语句实现对员工信息的现增删改查,并打印结果。
二、Python编程
导入需要的依赖库
# -*- coding: UTF-8 -*-
"""
@Author :叶庭云
@file :实训第二次作业
@function :封装函数 根据输入的文件路径和sql命令
模拟sql语句实现对员工信息的现增删改查
"""
import re
import os
函数式编程
def sql_parse(sql_, key_list):
"""
解析sql命令字符串,按照key_lis列表里的元素分割sql得到字典形式的命令sql_dic
:param sql_:
:param key_list:
:return:
"""
sql_list = []
sql_dic = {}
for i in key_list:
b = [j.strip() for j in sql_.split(i)]
if len(b) > 1:
if len(sql_.split('limit')) > 1:
sql_dic['limit'] = sql_.split('limit')[-1]
if i == 'where' or i == 'values':
sql_dic[i] = b[-1]
if sql_list:
sql_dic[sql_list[-1]] = b[0]
sql_list.append(i)
sql_ = b[-1]
else:
sql_ = b[0]
if sql_dic.get('select'):
if not sql_dic.get('from') and not sql_dic.get('where'):
sql_dic['from'] = b[-1]
if sql_dic.get('select'):
sql_dic['select'] = sql_dic.get('select').split(',')
if sql_dic.get('where'):
sql_dic['where'] = where_parse(sql_dic.get('where'))
return sql_dic
def where_parse(where):
"""
格式化where字符串为列表where_list,用'and', 'or', 'not'分割字符串
:param where:
:return:
"""
casual_l = [where]
logic_key = ['and', 'or', 'not']
for j in logic_key:
for i in casual_l:
if i not in logic_key:
if len(i.split(j)) > 1:
ele = i.split(j)
index = casual_l.index(i)
casual_l.pop(index)
casual_l.insert(index, ele[0])
casual_l.insert(index + 1, j)
casual_l.insert(index + 2, ele[1])
casual_l = [k for k in casual_l if k]
where_list = three_parse(casual_l, logic_key)
return where_list
def three_parse(casual_l, logic_key):
"""
处理临时列表casual_l中具体的条件,'staff_id>5'-->['staff_id','>','5']
:param casual_l:
:param logic_key:
:return:
"""
where_list = []
for i in casual_l:
if i not in logic_key:
b = i.split('like')
if len(b) > 1:
b.insert(1, 'like')
where_list.append(b)
else:
key = ['<', '=', '>']
new_lis = []
opt = ''
lis = [j for j in re.split('([=<>])', i) if j]
for k in lis:
if k in key:
opt += k
else:
new_lis.append(k)
new_lis.insert(1, opt)
where_list.append(new_lis)
else:
where_list.append(i)
return where_list
def sql_action(sql_dic, titles):
"""
把解析好的sql_dic分发给相应函数执行处理
:param sql_dic:
:param titles:
:return:
"""
key = {'select': select,
'insert': insert,
'delete': delete,
'update': update}
res = []
for i in sql_dic:
if i in key:
res = key[i](sql_dic, titles)
return res
def select(sql_dic, title_):
"""
处理select语句命令
:param sql_dic:
:param title_:
:return:
"""
with open(data_path, 'r', encoding='utf-8') as fh:
filter_res = where_action(fh, sql_dic.get('where'), title_)
limit_res = limit_action(filter_res, sql_dic.get('limit'))
search_res = search_action(limit_res, sql_dic.get('select'), title_)
return search_res
def insert(sql_dic):
"""
处理insert语句命令
:param sql_dic:
:return:
"""
with open(data_path, 'r+', encoding='utf-8') as fp:
data_ = fp.readlines()
phone_list = [i.strip().split(',')[4] for i in data_]
ins_count = 0
if not data_:
new_id = 1
else:
last = data_[-1]
last_id = int(last.split(',')[0])
new_id = last_id + 1
record = sql_dic.get('values').split('/')
for i in record:
if i.split(',')[2] in phone_list:
print('