|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如何实现 录入一条信息后,继续录入下一条信息?
查询一条信息后,继续查询下一条信息?
import redis
ip = '127.0.0.1'
conn_pool = redis.ConnectionPool(host=ip,port=6379,decode_responses=True)
r = redis.Redis(connection_pool=conn_pool)
name = "zhangsan"
h=1.8
w=50
district="山西测试"
obj={'h':h,'w':w,'district':district}
r.hset("record",name,str(obj))
res=r.hget("record",name)
res_obj=eval(res)
print("姓名:%s 身高:%s,体重:%d,地区:%s \r\n"%(name,res_obj["h"],res_obj["w"],res_obj["district"]))
function=input("请选择功能(1录入,2查询):")
if int(function)==2:
#选择了查询
name = input("请输入查询姓名:")
res = r.hget("record",name)
if res is None:
print("未查询到结果")
else:
res_obj=eval(res)
print("未查询到结果吗?")
print("姓名:%s 身高:%s,体重:%s,地区:%s \r\n"%(name,res_obj["h"],res_obj["w"],res_obj["district"]))
else:
#################################
print("开始录入\r\n")
name = input("请输入姓名格式 abc:")
h=input("请输入身高格式.1.8:")
w=input("请输入体重格式66:")
district=input("请输入地区shanghai:")
obj={'h':h,'w':w,'district':district}
r.hset("record",name,str(obj))
res=r.hget("record",name)
print("最后一行代码")
加个whie循环就好 import redis
ip = '127.0.0.1'
conn_pool = redis.ConnectionPool(host=ip, port=6379, decode_responses=True)
r = redis.Redis(connection_pool=conn_pool)
name = "zhangsan"
h = 1.8
w = 50
district = "山西测试"
obj = {'h': h, 'w': w, 'district': district}
r.hset("record", name, str(obj))
res = r.hget("record", name)
res_obj = eval(res)
print("姓名:%s 身高:%s,体重:%d,地区:%s \r\n" %
(name, res_obj["h"], res_obj["w"], res_obj["district"]))
while True:
function = input("请选择功能(1录入,2查询, 3退出):")
if int(function) == 2:
# 选择了查询
name = input("请输入查询姓名:")
res = r.hget("record", name)
if res is None:
print("未查询到结果")
else:
res_obj = eval(res)
print("未查询到结果吗?")
print("姓名:%s 身高:%s,体重:%s,地区:%s \r\n" %
(name, res_obj["h"], res_obj["w"], res_obj["district"]))
elif int(function) == 1:
#################################
print("开始录入\r\n")
name = input("请输入姓名格式 abc:")
h = input("请输入身高格式.1.8:")
w = input("请输入体重格式66:")
district = input("请输入地区shanghai:")
obj = {'h': h, 'w': w, 'district': district}
r.hset("record", name, str(obj))
res = r.hget("record", name)
print("最后一行代码")
else:
break
|
|