python增删改查界面_python实现数据库增删改查

column_dic = {"id": 0, "name": 1, "age": 2, "phone": 3, "job": 4}

def get_show_list(col_condition): # "select age,name"

"""

获取要展示的列名

:param col_condition: 用户输入的select条件

:return: 列名组成的列表

"""

col_info_list = col_condition.strip().split("select") # ["", "age,name"]

col_info_list = [col_info_item for col_info_item in col_info_list if col_info_item.strip()] # ["age,name"]

if col_info_list:

col_info = col_info_list[0].strip() # "age,name"

if "*" in col_info:

return column_dic.keys()

elif col_info:

ret = col_info.split(",") # [" age ","name"]

return [item.strip() for item in ret] # ["age","name"]

else:

print(col_info)

def filter_handler(operate, con): # ">" , "age>22"

selected_list = []

col, val = con.split(operate)

col = col.strip()

val = val.strip()

judge = "int(line_list[column_dic[col]]) %s int(val)" % operate if operate == "<" or operate == ">" \

else "line_list[column_dic[col]] %s val" % operate

f = open("student", encoding="utf-8")

for line in f:

line_list = line.strip().split(",")

if eval(judge):

selected_list.append(line_list)

f.close()

return selected_list

def get_selected_line(con): # con = "age>22"

"""

获取要查找的行,并将每一行作为列表项存储到selected_list

:param con:

:return:

"""

if ">" in con:

selected_list = filter_handler(">", con)

elif "<" in con:

selected_list = filter_handler("<", con)

elif "=" in con:

selected_list = filter_handler("==", con.replace("=", '=='))

elif "like" in con:

selected_list = filter_handler("in", con)

return selected_list

def show(selected_list, show_list):

for selected_item in selected_list:

for col in show_list:

print(selected_item[column_dic[col]], end=" ")

print()

condition = input(">>>") # "select age,name where age>22"

ret = condition.split('where') # ["select age,name", "age>22"]

con = ret[1].strip() # "age>22"

show_list = get_show_list(ret[0]) # ["name", "age"]

selected_list = get_selected_line(con)

show(selected_list, show_list)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值