# 文件存储时可以这样表示
#
# 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. 【原】无脑操作:ElasticSearch学习笔记(01)

    开篇来自于经典的“保安的哲学三问”(你是谁,在哪儿,要干嘛) 问题一.ElasticSearch是什么?有什么用处? 答:截至2018年12月28日,从ElasticSearch官网(https:// ...

  2. markdown 基本操作

    无序列表:输入-之后输入空格有序列表:输入数字+“.”之后输入空格任务列表:-[空格]空格 文字标题:ctrl+数字表格:ctrl+t生成目录:[TOC]按回车选中一整行:ctrl+l选中单词:ctr ...

  3. F#正则表达式

    此词法分析器允许您使用F#计算表达式以非常声明的方式定义基于正则表达式的规则. F# 打开 Lexer 让 定义= lexerDefinitions { 做!addNextlineDefinition ...

  4. 用pyinstaller打包python程序,解决打包时的错误:Cannot find existing PyQt5 plugin directories

    解决方法就是用everything搜索PyQt5,找到 /Library/plugins路径下的PyQt5文件夹,将里面的dll动态库pyqt5qmlplugin.dll复制出来 按照错误提示的路径, ...

  5. Java微服务之Spring Boot on Docker

    本文学习前提:Java, Spring Boot, Docker, Spring Cloud 一.准备工作 1.1 安装Docker环境 这一部分请参考我的另一篇文章<ASP.NET Core ...

  6. Springboot整合Elastic-Job(二)

    上文我们讲到Springboot整合Elastic-Job整合的demo,只是简单的实现了主要功能.本文在上文基础上,进行新的调整. 事件追踪 Elastic-Job提供了事件追踪功能,可通过事件订阅 ...

  7. k邻近算法(KNN)实例

    一 k近邻算法原理 k近邻算法是一种基本分类和回归方法. 原理:K近邻算法,即是给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的K个实例,这K个实例的多数属于某个类,就把该输入实 ...

  8. SLAM+语音机器人DIY系列:(二)ROS入门——8.理解roslaunch在大型项目中的作用

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  9. jmFidExt - Fiddler 代理插件

    本插件作用是把某些请求代理到指定的IP(端号)或文件,设置简便. 源码地址:https://github.com/jiamao/jmFidExt 示图 安装 下载代码,用开发工具vs2003及以上的版 ...

  10. 如何使用git和ssh部署本地代码到服务器

    一.首先设置好自己本地的Git用户名和密码: git config --global user.name "your name" git config --global user. ...