Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis database-python interaction

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

-install-install package ① enter the virtual environment py2_db, network installation package redispip install redis ② Chinese official website (http://redis.cn/clients.html) download redis source code install unzip redis-py-master.zipcd redis-py-masterpython setup.py install-- call module from redis import *-StrictRedis object method-- create an object through init, specify parameters host and port to connect with the specified server and port. Host defaults to localhost. Port defaults to 6379 color-depending on the type, there are unused instance methods that can be called, corresponding to the redis command you learned earlier, and the parameters required by the method are the same as those of the command. -- instance method:-- 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- string- adds from redis import * if _ _ name__ = = "_ _ main__": try: # create StrictRedis object Establish a link with the redis server sr = StrictRedis () # add the key py1, the value is gj result = sr.set ('py1',' gj') # output the response result, and return True if the addition is successful Otherwise, return false print (result) except Exception as e: print (e)-string- to get from redis import * if _ _ name__ = = "_ _ main__": try: # create a StrictRedis object and establish a link with the redis server sr = StrictRedis () # get the key py1 value result = sr.get ('py1') # output key value If the key does not exist, return None print (result) except Exception as e: print (e)-string- modify from redis import * if _ _ name__ = = "_ _ main__": try: # create a StrictRedis object, establish a link with the redis server sr = StrictRedis () # set the py1 value, and modify it if the key already exists If the key does not exist, add result = sr.set ('py1',' hr') # to output the result. If the operation is successful, return True, otherwise return False print (result) except Exception as e: print (e)-string- delete from redis import * if _ _ name__ = = "_ _ main__": try: # create StrictRedis object Establish a link with the redis server sr = StrictRedis () # set the value of the key py1, modify it if the key already exists, add result = sr.delete ('py1') # if the key does not exist, output the corresponding result, and return the number of keys affected if the deletion is successful Otherwise, 0 print (result) except Exception as e: print (e)-get key from redis import * if _ _ name__ = = "_ _ main__": try: # create StrictRedis object, establish link with redis server sr = StrictRedis () # get all keys result = sr.keys () # output response result All the keys form a list. If there are no keys, return an empty list print (result) except Exception as e: print (e)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report