首页 > 编程语言 > 详细

python操作redis

时间:2019-12-19 20:40:44      阅读:92      评论:0      收藏:0      [点我收藏+]
python操作redis

string类型:
 1 import redis
 2 r = redis.Redis(host=ip,port=6379,password=123456,db=0)  # 建立连接
 3 r.set(name1,qq)  # 设置值
 4 r.set(name2,测试)
 5 res = r.get(name1)  # 获取值,返回的是bytes类型数据
 6 res2 = r.get(name1).decode()  # decode解码
 7 print(res)  # 打印结果
 8 print(res2)
 9 print(r.keys())# 获取所有的key
10 for k in r.keys():  # 遍历所有的key,打印key和value
11     print({k}:{v}.format(k=k.decode(),v=r.get(k).decode()))
12 print(r.keys(n*))  # 获取n开头的key
运行结果:
bqq
qq
[bname2, bname1]
name2:测试
name1:qq
[bname2, bname1]

 

 hash类型:

1 # hash类型
2 r.hset(test1,age,18)  # 设置值
3 print(r.hget(test1,age).decode())  # 获取值
4 
5 res = r.hgetall(test1)  # 获取hash类型这个name里面所有的数据
6 print(res)
7 print(res[age.encode()])  # 因为key是bytes类型,所以,这里需要encode
8 print(res[age.encode()].decode())  # 因为key是bytes类型,所以,这里需要encode

结果:

18
{bage: b18}
b18
18

参考:https://www.cnblogs.com/UncleYong/p/10950727.html

python操作redis

原文:https://www.cnblogs.com/aiyumo/p/12069619.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!