
利用python模擬sql語句對(duì)員工表格進(jìn)行增刪改查
本文主要給大家介紹了關(guān)于python模擬sql語句對(duì)員工表格進(jìn)行增刪改查的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:
具體需求:
員工信息表程序,實(shí)現(xiàn)增刪改查操作:
可進(jìn)行模糊查詢,語法支持下面3種:
select name,age from staff_data where age > 22 多個(gè)查詢參數(shù)name,age 用','分割
select * from staff_data where dept = 人事
select * from staff_data where enroll_date like 2013
查到的信息,打印后,最后面還要顯示查到的條數(shù)
可創(chuàng)建新員工紀(jì)錄,以phone做唯一鍵,phone存在即提示,staff_id需自增,添加多個(gè)記錄record1/record2中間用'/'分割
insert into staff_data values record1/record2
可刪除指定員工信息紀(jì)錄,輸入員工id,即可刪除
delete from staff_data where staff_id>=5andstaff_id<=10
可修改員工信息,語法如下:
update staff_table set dept=Market,phone=13566677787 where dept = 運(yùn)維 多個(gè)set值用','分割
使用re模塊,os模塊,充分使用函數(shù)精簡代碼,熟練使用str.split()來解析格式化字符串
由于,sql命令中的幾個(gè)關(guān)鍵字符串有一定規(guī)律,只出現(xiàn)一次,并且有順序!!!
按照key_lis = ['select', 'insert', 'delete', 'update', 'from', 'into', 'set', 'values', 'where', 'limit']的元素順序分割sql.
分割元素作為sql_dic字典的key放進(jìn)字典中.分割后的列表為b,如果len(b)>1,說明sql字符串中含有分割元素,同時(shí)b[0]對(duì)應(yīng)上一個(gè)分割元素的值,b[-1]為下一次分割對(duì)象!
這樣不斷迭代直到把sql按出現(xiàn)的所有分割元素分割完畢,但注意這里每次循環(huán)都是先分割后賦值!!!當(dāng)前分割元素比如'select'對(duì)應(yīng)的值,需要等到下一個(gè)分割元素
比如'from'執(zhí)行分割后的列表b,其中b[0]的值才會(huì)賦值給sql_dic['select'],所以最后一個(gè)分割元素的值,不能通過上述循環(huán)來完成,必須先處理可能是最后一個(gè)分割元素,再正常循環(huán)!!
在這sql語句中,有可能成為最后一個(gè)分割元素的 'limit' ,'values', 'where', 按優(yōu)先級(jí)別,先處理'limit' ,再處理'values'或 'where'.....
處理完得到sql_dic后,就是你按不同命令執(zhí)行,對(duì)數(shù)據(jù)文件的增刪改查,最后返回處理結(jié)果!!
示例代碼
# _*_coding:utf-8_*_
# Author:Jaye He
import re
import os
def sql_parse(sql, key_lis):
'''
解析sql命令字符串,按照key_lis列表里的元素分割sql得到字典形式的命令sql_dic
:param sql:
:param key_lis:
:return:
'''
sql_list = []
sql_dic = {}
for i in key_lis:
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):
'''
處理臨時(shí)列表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, title):
'''
把解析好的sql_dic分發(fā)給相應(yīng)函數(shù)執(zhí)行處理
:param sql_dic:
:param title:
: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, title)
return res
def select(sql_dic, title):
'''
處理select語句命令
:param sql_dic:
:param title:
:return:
'''
with open('staff_data', '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, title):
'''
處理insert語句命令
:param sql_dic:
:param title:
:return:
'''
with open('staff_data', 'r+', encoding='utf-8') as f:
data = f.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(',')[3] in phone_list:
print('\033[1;31m%s 手機(jī)號(hào)已存在\033[0m' % i)
else:
new_record = '%s,%s\n' % (str(new_id), i)
f.write(new_record)
new_id += 1
ins_count += 1
f.flush()
return ['insert successful'], [str(ins_count)]
def delete(sql_dic, title):
'''
處理delete語句命令
:param sql_dic:
:param title:
:return:
'''
with open('staff_data', 'r', encoding='utf-8') as r_file,\
open('staff_data_bak', 'w', encoding='utf-8') as w_file:
del_count = 0
for line in r_file:
dic = dict(zip(title.split(','), line.split(',')))
filter_res = logic_action(dic, sql_dic.get('where'))
if not filter_res:
w_file.write(line)
else:
del_count += 1
w_file.flush()
os.remove('staff_data')
os.rename('staff_data_bak', 'staff_data')
return ['delete successful'], [str(del_count)]
def update(sql_dic, title):
'''
處理update語句命令
:param sql_dic:
:param title:
:return:
'''
set_l = sql_dic.get('set').strip().split(',')
set_list = [i.split('=') for i in set_l]
update_count = 0
with open('staff_data', 'r', encoding='utf-8') as r_file,\
open('staff_data_bak', 'w', encoding='utf-8') as w_file:
for line in r_file:
dic = dict(zip(title.split(','), line.strip().split(',')))
filter_res = logic_action(dic, sql_dic.get('where'))
if filter_res:
for i in set_list:
k = i[0]
v = i[-1]
dic[k] = v
line = [dic[i] for i in title.split(',')]
update_count += 1
line = ','.join(line)+'\n'
w_file.write(line)
w_file.flush()
os.remove('staff_data')
os.rename('staff_data_bak', 'staff_data')
return ['update successful'], [str(update_count)]
def where_action(fh, where_list, title):
'''
具體處理where_list里的所有條件
:param fh:
:param where_list:
:param title:
:return:
'''
res = []
if len(where_list) != 0:
for line in fh:
dic = dict(zip(title.split(','), line.strip().split(',')))
if dic['name'] != 'name':
logic_res = logic_action(dic, where_list)
if logic_res:
res.append(line.strip().split(','))
else:
res = [i.split(',') for i in fh.readlines()]
return res
pass
def logic_action(dic, where_list):
'''
判斷數(shù)據(jù)文件中每一條是否符合where_list條件
:param dic:
:param where_list:
:return:
'''
logic = []
for exp in where_list:
if type(exp) is list:
exp_k, opt, exp_v = exp
if exp[1] == '=':
opt = '=='
logical_char = "'%s'%s'%s'" % (dic[exp_k], opt, exp_v)
if opt != 'like':
exp = str(eval(logical_char))
else:
if exp_v in dic[exp_k]:
exp = 'True'
else:
exp = 'False'
logic.append(exp)
res = eval(' '.join(logic))
return res
def limit_action(filter_res, limit_l):
'''
用列表切分處理顯示符合條件的數(shù)量
:param filter_res:
:param limit_l:
:return:
'''
if limit_l:
index = int(limit_l[0])
res = filter_res[:index]
else:
res = filter_res
return res
def search_action(limit_res, select_list, title):
'''
處理需要查詢并顯示的title和相應(yīng)數(shù)據(jù)
:param limit_res:
:param select_list:
:param title:
:return:
'''
res = []
fields_list = title.split(',')
if select_list[0] == '*':
res = limit_res
else:
fields_list = select_list
for data in limit_res:
dic = dict(zip(title.split(','), data))
r_l = []
for i in fields_list:
r_l.append((dic[i].strip()))
res.append(r_l)
return fields_list, res
if __name__ == '__main__':
with open('staff_data', 'r', encoding='utf-8') as f:
title = f.readline().strip()
key_lis = ['select', 'insert', 'delete', 'update', 'from', 'into', 'set', 'values', 'where', 'limit']
while True:
sql = input('請(qǐng)輸入sql命令,退出請(qǐng)輸入exit:').strip()
sql = re.sub(' ', '', sql)
if len(sql) == 0:continue
if sql == 'exit':break
sql_dict = sql_parse(sql, key_lis)
fields_list, fields_data = sql_action(sql_dict, title)
print('\033[1;33m結(jié)果如下:\033[0m')
print('-'.join(fields_list))
for data in fields_data:
print('-'.join(data))
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03