# 文件存储时可以这样表示
#
# 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. postgres的使用命令

    1.更新源 yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos1 ...

  2. kubernetes 安装备注

    一.安装环境 阿里云:centos 7.3 master节点:外网IP(116.62.205.90).内网IP(172.16.223.200) node节点:外网IP(116.62.212.174). ...

  3. python 基础语法梳理(二)

    1.gevent使用 # -*- coding: utf-8 -*- import gevent import platform from gevent import subprocess def _ ...

  4. GitHub的Repository权限将public转为private

    2019年1月7日,GitHub CEO Nat Friedman 于官方博客公开发文,称“New year, new GitHub”,宣布从此将免费无限地为普通用户提供私有仓库服务. 因此,我们可以 ...

  5. RecyclerView实现一个页面有多种item,每个item有多个view,并且可以让任意item的任意view自定义监听,通过接口方法进行触发操作

    百度了很多贴子,看着大佬的博客,模仿尝试,最终都是以失败告终,api可能版本不一样, 毕竟博客大佬都是7~8前写的,日期新点的都是好几年前了,多次尝试,还是报出莫名其妙的错. 哎,忧伤. 翻阅各种资料 ...

  6. 机器学习中如何处理不平衡数据(imbalanced data)?

    推荐一篇英文的博客: 8 Tactics to Combat Imbalanced Classes in Your Machine Learning Dataset 1.不平衡数据集带来的影响 一个不 ...

  7. nginx.conf添加lua.conf配置

    1.在nginx的conf下配置lua.conf......vi lua.conf server { listen ; server_name _; location /lua { default_t ...

  8. Redhat 平台下 LVM 管理说明

    Redhat 平台下  LVM 管理说明 LVM 是 Logical Volume Manager(逻辑卷管理器)的简写,它为主机提供了更高层次的磁盘存储管理能力.LVM 可以帮助系统管理员为应用与用 ...

  9. 『集群』001 Slithice 服务器集群 概述

    Slithice 服务器集群 概述 Slithice是做什么的 Slithice 是一个 跨平台 的 分布式架构 框架: 旨在简化 分布式开发 的开发难度,节省 开发成本 和 后期维护成本: 并提供 ...

  10. springboot~yml里的自定义配置

    主要介绍三种,字符串配置,数组配置和带默认值的配置 字符串配置 //yml setString: hello /** * 字符串. */ @Value("${setString}" ...