# 文件存储时可以这样表示
#
# 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. 【原】Oracle EBS 11无法打开Form及Form显示乱码的解决

    问题:Oracle EBS 11无法打开Form及Form显示乱码 解决: 1.尝试使用jre1.5或1.6安装目录下jre/bin/server目录里的jvm.dll替换JInitiator安装目录 ...

  2. ASP.NET Aries 高级开发教程:使用存储过程(番外篇)

    前言: 发现这个问题,有不少人提起过,所以就简单写成文章吧. 接下来看如何在Aries 框架中使用存储过程,整体步骤和绑定普通视图差不多. 步骤一:新建一个空视图. 可以在SqlCode管理中,创建一 ...

  3. 聊聊分布式开发 Spring Cloud

    概述 本文章只是简单介绍了微服务开发的一些关键词,如果需要知道具体实现和可以评论留言 我会及时的增加连接写出具体实现(感觉没人看 就没写具体实现). 持续更新中...... SpringCloud和D ...

  4. pandas操作

    python中使用了pandas的一些操作,特此记录下来: 生成DataFrame import pandas as pd data = pd.DataFrame({ 'v_id': ["v ...

  5. PERL学习笔记---正则表达式的应用

    使用m//匹配 //这是m//(模式匹配)的一种简写.同qw//操作一样,可以使用任何成对的分隔符.因此,可以使用m(fred), m<fred>, m{fred}, m[fred],或者 ...

  6. ABP框架连接Mysql数据库

    开始想用Abp框架来搭建公司的新项目,虽然一切还没有定数,但是兵马未动,粮草先行,我先尝试一下整个过程,才能够更好的去争取机会. 此次技术选型:Abp(Asp.Net core mvc)+mysql( ...

  7. 西安活动 | 4月20日「拥抱开源,又见.NET :云时代 • 新契机」

    云计算日渐兴起,成为提升企业效率和生产力的最终解决方案,而云时代也为软件开发模式带来了翻天覆地的变化.可以说 .NET Core就是这个时代催生的产物.自2016年 .NET Core 1.0 发布以 ...

  8. c# Base64解密加密

    private static string base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ...

  9. acrobat pdf 按页拆分

    百度 https://jingyan.baidu.com/article/37bce2be7098a21002f3a2f2.html 百度acrobat版本比我的高,操作不同了: 我的方案: 组织页面 ...

  10. headfirst设计模式(5)—工厂模式体系分析及抽象工厂模式

    先编一个这么久不写的理由 上周我终于鼓起勇气翻开了headfirst设计模式这本书,看看自己下一个设计模式要写个啥,然后,我终于知道我为啥这么久都没写设计模式了,headfirst的这个抽象工厂模式, ...