学习目标
- 能够使用StrictRedis对象对string类型数据进行增删改查
准备
- 在桌面上创建redis目录
- 使用pycharm打开 redis目录
- 创建redis_string.py文件
from redis import *
if __name__=="__main__":
try:
sr=StrictRedis()
except Exception as e:
print(e)
string-增加
- ⽅法set,添加键、值,如果添加成功则返回True,如果添加失败则返回False
- 编写代码如下
from redis import *
if __name__=="__main__":
try:
sr=StrictRedis()
result=sr.set('name','itheima')
print(result)
except Exception as e:
print(e)
string-获取
- ⽅法get,添加键对应的值,如果键存在则返回对应的值,如果键不存在则返回None
- 编写代码如下
from redis import *
if __name__=="__main__":
try:
sr=StrictRedis()
result = sr.get('name')
print(result)
except Exception as e:
print(e)
string-修改
- ⽅法set,如果键已经存在则进⾏修改,如果键不存在则进⾏添加
- 编写代码如下
from redis import *
if __name__=="__main__":
try:
sr=StrictRedis()
result = sr.set('name','itcast')
print(result)
except Exception as e:
print(e)
string-删除
- ⽅法delete,删除键及对应的值,如果删除成功则返回受影响的键数,否则则返 回0
- 编写代码如下
from redis import *
if __name__=="__main__":
try:
sr=StrictRedis()
result = sr.delete('name')
print(result)
except Exception as e:
print(e)
获取键
from redis import *
if __name__=="__main__":
try:
sr=StrictRedis()
result=sr.keys()
print(result)
except Exception as e:
print(e)