表内容有以下列:
id,name,age,phone,dept,enroll_date
//db1 数据库名 //emp表名 数据库表名和对应文件要关联上。
通过PYTHON脚本模拟实现以下SQL语句:
增删改查
sql>select
sql>select * from db1.emp
sql>select * from db1.emp limit 3 // 查前3条
sql>select * from db1.emp where id > 24
sql>select * from db1.emp where name like 李
sql>select * from db1.emp where id >10 and id < 14 or name like 李
sql>select * from db1.emp where not id > 3
sql>select name from db1.emp
sql>select name from db1.emp where
查询到的信息以列表的形式显示出来
sql>insert id 递增 以phone做唯一键,phone存在即提示
sql>insert into db1.emp values ‘Tom,23,13212323276,运维,2017-05-16‘
sql>insert successful
delete
sql>delete from db1.emp where id > 25
sql>delete successful
update update staff_table set dept=Market,phone=13566677787 where dept = 运维
sql>update db1.emp set name=‘sb‘ where id=24
sql>update db1.emp set name=‘alex‘ where name like sb
sql>update successful
sql>exit
脚本内容如下:
##################################################################
#!/usr/bin/env python
# _*_ coding:utf-8 -*-
import sys
test_db_file=‘db1/emp‘
key_word = [‘select‘, ‘insert‘, ‘delete‘, ‘update‘, ‘from‘, ‘into‘, ‘set‘, ‘values‘, ‘where‘, ‘limit‘]
where_list=[‘>‘,‘<‘,‘like‘]
logic_list=[‘and‘,‘or‘,‘not‘]
# 判断有没有where
def check_where(db_ff,sql_ii):
#db_ff为行内容,sql_ii为SQL语句的列表
# 判断SQL中是否有where,并提取where后面的语句。
if ‘where‘ in sql_ii:
sel_index = int(sql_ii.index(‘where‘)) + 1
where_info = sql_ii[sel_index:]
#print(where_info)
# 判断有没有逻辑条件。
k=0
for key_w in logic_list:
if key_w in where_info:
print(key_w)
k=1
if k==0:
print(where_info)
else:
return db_ff
def file_all():
# 提取所有行,返回一个列表OK。
db_f = []
with open(test_db_file, encoding=‘utf-8‘) as db_file:
db_f1 = db_file.readlines()
for f in db_f1:
f = f.strip().split(‘,‘)
db_f.append(f)
return db_f
def limit_row(sel_num):
# 提取limit限制的行数, sel_num为行数,OK
db_f=file_all()
limit_f=[]
# sql_num 要显示的行数
sel_num = int(sel_num)
# 判断显示的行数要小于等于表的总行数。
if len(db_f) >= sel_num:
for i in range(0, sel_num):
limit_f.append(db_f[i])
return limit_f
else:
print("Not have %s row in table! " % sel_num)
return limit_f
def get_column(column_name,db_ff):
# 提取某一列column_name为列名,db_ff为提取出来的行内容。OK
column_names = file_all()[0] ## 获取表的列名
# print(column_names)
if column_name in column_names:
column_nu = column_names.index(column_name)
for db_fff in db_ff:
print(db_fff[column_nu])
def db_select(select,*sql_i):
for sql_ii in sql_i:
pass
#print(sql_ii)
# 取匹配行的所有内容
if sql_ii[0] == ‘*‘:
db_ff=file_all()
#print(db_f)
# 判断SQL里是否有limit限制,如果有就获取限制的行数。
if ‘limit‘ in sql_ii:
# 获取限制的行数
sel_index=int(sql_ii.index(‘limit‘))+1
sel_num=sql_ii[sel_index]
#print(sel_num)
db_ff=limit_row(sel_num)
print(db_ff)
else:
print(db_ff)
check_where(db_ff, sql_ii)
# 取匹配行的某一列
else:
# 先判读是取所有行,还是取limit限制的行,然后从这些行中提取某一列
if ‘limit‘ in sql_ii:
# 获取限制的行数
sel_index=int(sql_ii.index(‘limit‘))+1
sel_num=sql_ii[sel_index]
db_ff=limit_row(sel_num)
else:
db_ff = file_all()
print(db_ff)
get_column(sql_ii[0], db_ff)
#pass
##########################################################
def db_delete():
pass
def db_insert():
pass
def db_update():
pass
if __name__ == "__main__":
#print(file_all())
#print (limit_row(file_all(), ‘2‘))
#a=db_select()
#print(a)
while True:
sql_info=input("sql>")
sql_info=sql_info.strip().split()
print("sql_info:", sql_info)
if sql_info[0].lower() == ‘quit‘ or sql_info[0].lower() == ‘exit‘:
print(‘退出‘)
sys.exit(0)
elif sql_info[0].lower()==‘select‘:
db_select(‘select‘,sql_info[1:])