Redis与Python的交互

Posted 胡说八道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis与Python的交互相关的知识,希望对你有一定的参考价值。

# 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与Python的交互

redis与python交互

Redis与Python进行交互

python与redis交互

Redis与Python交互

如何使用 xcode 将快照划分为多个片段,以便让用户与每个片段进行交互?