# 文件存储时可以这样表示
#
# 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)

练习 python之数据库增删改查的更多相关文章

  1. python实现数据库增删改查

    column_dic = {"id": 0, "name": 1, "age": 2, "phone": 3, &quo ...

  2. python操作mysql数据库增删改查的dbutils实例

    python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...

  3. Python实现mysql数据库增删改查

    利用python操作mysql数据库用法简单,环境配置容易,本文将实现对库增.删.改.查的简易封装!   1. 环境配置 安装第三方包  ,导入模块 mysql.connector  pip inst ...

  4. 数据库学习之数据库增删改查(另外解决Mysql在linux下不能插入中文的问题)(二)

    数据库增删改查 增加 首先我们创建一个数据库user,然后创建一张表employee create table employee( id int primary key auto_increment, ...

  5. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  6. 2. MongoDB基本操作 —— 用Mongo.exe操作数据库增删改查

    一.开篇 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象 ...

  7. go——beego的数据库增删改查

    一直都不理解使用go语言的时候,为什么还要自己去装beego,以为使用go便可以解决所有的问题,结果在朋友的点拨下,才意识到: go与beego的关系就好比是nodejs与thinkjs的关系,因此也 ...

  8. (转)SQLite数据库增删改查操作

    原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...

  9. Yii2.0高级框架数据库增删改查的一些操作

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

随机推荐

  1. NTSTATUS Values

    By combining the NTSTATUS into a single 32-bit numbering space, the following NTSTATUS values are de ...

  2. Python初识+条件语句+循环语句

    一.写照: 1.第一个程序 hello world print('hello world') 后缀名可以是任意(只是现在)(lx.py lx.ps) 导入模块时不是.py 就会出错 2.解释器路径 # ...

  3. 普通程序员如何转向AI方向(转)

    普通程序员如何转向AI方向   眼下,人工智能已经成为越来越火的一个方向.普通程序员,如何转向人工智能方向,是知乎上的一个问题.本文是我对此问题的一个回答的归档版.相比原回答有所内容增加. 一. 目的 ...

  4. Android 音视频同步机制

    一.概述 音视频同步(avsync),是影响多媒体应用体验质量的一个重要因素.而我们在看到音视频同步的时候,最先想到的就是对齐两者的pts,但是实际使用中的各类播放器,其音视频同步机制都比这些复杂的多 ...

  5. 从壹开始前后端分离 [.netCore 不定期更新 ] 三十五║ 完美实现全局异常日志记录

    缘起 哈喽我是不定期更新的日常,昨天群里小伙伴问到了记录日志,当然,以前我也挖过这个坑,后来一直没有来得及填上,也想着 swagger 一直又有错误信息展示的功能,就迟迟没有添加这个功能,不过昨天夜里 ...

  6. Android APP应用启动页白屏(StartingWindow)优化

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 StartingWindow 的处理方式: 使用系统默认的 StartingWindow :用户点了应用图标启动应用,马上弹出系统默 ...

  7. ubuntu上配置nginx实现反向代理

    反向代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客 ...

  8. springboot~Compiler时开启插件的注解功能

    对于IJ这个IDE工具来说,我们会安装一些插件来帮助我们更好的进行开发,像lombok就是一款不错的插件,使用注解的方式在项目编译时帮助我们生成代码,像getter,setter,tostring等等 ...

  9. Spring Boot分布式系统实践【基础模块构建3.3】注解轻松实现操作日志记录

    日志注解 前言 spring切面的编程,spring中事物处理.日志记录常常与pointcut相结合 * * Pointcut 是指那些方法需要被执行"AOP",是由"P ...

  10. 全文检索-Elasticsearch (四) elasticsearch.net 客户端

    本篇摘自elasticsearch.net search入门使用指南中文版(翻译) 原文:http://edu.dmeiyang.com/book/nestusing.html elasticsear ...