1. 程式人生 > >python練習題

python練習題

python 練習題

表內容有以下列:
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:])

本文出自 “鵬哥玩linux” 博客,轉載請與作者聯系!

python練習題