码迷,mamicode.com
首页 > 编程语言 > 详细

Redis与Python的交互

时间:2017-12-03 17:16:44      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:交互   redis   服务   pytho   响应   als   col   delete   数据   

# 1.通过python对Redis数据进行增删改查,获取键
1.增
    from redis import *


    if __name__ == "__main__":
        try:
            # 创建StrictRedis对象,与redis服务器建立链接
            sr = StrictRedis()
            # 添加键py1,值为gj
            result = sr.set(py1, gj)
            # 输出响应结果,如果添加成功则返回True,否则返回false
            print(result)
        except Exception as e:
            print(e)
2.删
    from redis import *


    if __name__ == "__main__":
        try:
            # 创建StrictRedis对象,与redis服务器建立链接
            sr = StrictRedis()
            # 设置键py1的值,如果键已经存在则进行修改,如果键不存在则进行添加
            result = sr.delete(py1)
            # 输出相应结果,如果删除成功则返回受影响的键数,否则则返回0
            print(result)
        except Exception as e:
            print(e)
3.改
    from redis import *


    if __name__ == "__main__":
        try:
            # 创建StrictRedis对象,与redis服务器建立链接
            sr = StrictRedis()
            # 设置py1的值,如果键已经存在则进行修改,如果键不存在则进行添加
            result = sr.set(py1, hr)
            # 输出相应结果,如果操作成功则返回True,否则返回False
            print(result)
        except Exception as e:
            print(e)
4.查
    from redis import *


    if __name__ == "__main__":
        try:
            # 创建StrictRedis对象,与redis服务器建立链接
            sr = StrictRedis()
            # 获取键py1的值
            result = sr.get(py1)
            # 输出键的值,如果键不存在则返回None
            print(result)
        except Exception as e:
            print(e)
5.获取键
    from redis import *


    if __name__ == "__main__":
        try:
            # 创建StrictRedis对象,与redis服务器建立链接
            sr = StrictRedis()
            # 获取所有的键
            result = sr.keys()
            # 输出响应结果,所有的键构成一个列表,如果没有键分返回空列表
            print(result)
        except Exception as e:
            print(e)
# 2.不同类型对应的实例方法
    --string:set setex mset append get mget
    --key:keys exists type delete expire getrange ttl
    --hash:hset hmset hkeys hget hmget hvals hdel
    --list:lpush rpush linsert lrange lset lrem
    --set:sadd smembers srem
    --zset:zadd zrange zrangebyscore zscore zrem zremrangebyscore

 

Redis与Python的交互

标签:交互   redis   服务   pytho   响应   als   col   delete   数据   

原文地址:http://www.cnblogs.com/pymkl/p/7966385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!