练习 python之数据库增删改查

# 文件存储时可以这样表示
#
# id,name,age,phone,dept,enroll_date
# 1,Alex Li,22,13651054608,IT,2013-04-01
# 2,Jack Wang,28,13451024608,HR,2015-01-07
# 3,Rain Wang,21,13451054608,IT,2017-04-01
# 4,Mack Qiao,44,15653354208,Sales,2016-02-01
# 5,Rachel Chen,23,13351024606,IT,2013-03-16
# 6,Eric Liu,19,18531054602,Marketing,2012-12-01
# 7,Chao Zhang,21,13235324334,Administration,2011-08-08
# 8,Kevin Chen,22,13151054603,Sales,2013-04-01
# 9,Shit Wen,20,13351024602,IT,2017-07-03
# 10,Shanshan Du,26,13698424612,Operation,2017-07-02
# 可进行模糊查询,语法至少支持下面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需自增语法:
#
# add to staff_table values Alex Li,25,134435344,IT,2015-10-29
# 可删除指定员工信息纪录,输入员工id,即可删除语法:
#
# del from staff_table where id = 3
# 可修改员工信息,语法如下:
#
# update staff_table set dept = Market where dept = IT #把所有dept=IT的纪录的dept改成Market
# update staff_table set age = 25 where name = Alex Li #把name=Alex Li的纪录的年龄改成25
# 以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。比如查询语句就显示查询出了多少条、修改语句就显示修改了多少条等。
# 注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码! 版本:python3.6 新建主体函数 databases.py
import re
import select_mo
import del_mo
import update_mo #operation = input('请输入你要操作的指令 :').lower()
operation = 'select name,age , phone from where age < 23'
SELECT = re.search('^select', operation)
DEL = re.search('^del', operation)
UPDATE = re.search('^update', operation) data = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'dept': 4, 'enroll_date': 5} if SELECT:
select_mo.SELECT(operation, data) elif DEL:
del_mo.DEL(operation, data) elif UPDATE:
update_mo.UPDATE(operation, data) else:
print('你输入的指令不正确')
新建查的脚本:select_mo.py
def SELECT(operation, data):
# select name,age from where age > 22
# select * from where dept = "IT"
# select * from where enroll_date like "2013"
# 1,Alex Li,22,13651054608,IT,2013-04-01 def print_name():
name = ''
for SCREEN in screen_list:
name += ' ' + data_list[data[SCREEN]]
print(name) check, term = operation.split('from')
screen = check.replace('select', '')
if 'where' in term:
term_total = term.replace('where', '')
if '>' in term_total:
term, tail = term_total.split('>')
elif '<' in term_total:
term, tail = term_total.split('<')
elif '=' in term_total:
term, tail = term_total.split('=')
tail = tail.replace('"','')
elif 'like' in term_total:
term, tail = term_total.split('like')
tail = tail.replace('"', '')
else:
exit('没有条件')
term = term.strip()
tail = tail.strip() screen_list = []
if '*' in screen:
screen_list = list(data)
elif ',' in screen:
screen_list = screen.split(',')
for i in range(len(screen_list)):
screen_list[i] = screen_list[i].strip()
else:
screen_list.append(screen.strip()) with open('database.txt') as DATA:
for i in DATA:
data_list = []
data_list = i.split(',')
for i in range(len(data_list)):
data_list[i] = data_list[i].strip()
#print(data_list)
if '>' in term_total:
if int(data_list[data[term]]) > int(tail):
print_name()
elif '<' in term_total:
if int(data_list[data[term]]) < int(tail):
print_name()
elif '=' in term_total and not data_list[data[term]].isdigit():
if data_list[data[term]] == tail:
print_name()
elif '=' in term_total and data_list[data[term]].isdigit():
if int(data_list[data[term]]) == int(tail):
print_name()
elif 'like' in term_total:
if tail in data_list[data[term]]:
print_name() if __name__ == '__main__' :
data = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'dept': 4, 'enroll_date': 5}
operation = 'select * from where enroll_date like "2013"'
SELECT(operation, data)
上一篇:在进行多次scanf时,printf输出错误


下一篇:JavaScript ECMAScript版本介绍