- Installation-Installation package ① Enter virtual Environment py2_db, networking installation package Redispip install redis② Chinese official website (http://redis.cn/clients.html) Download Redis Source Installation unzip redis -py -master.zipcd redis -py -masterpython setup.py install--Calling module From redis import *- strictredis object Method--Creating an object from Init, specifying the parameter host, Port is connected to the specified server and port, host defaults to localhost,port default to 6379--depending on the type, the owning instance method can be called, corresponding to the Redis command learned earlier, the method requires parameters that match the parameters of the command. --Example 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-Increase from redis import *if __name__ == "__main__": try: # Create StrictRedis objects, establishing links with Redis servers sr = strictredis () # add key py1 with a value of Gj result = sr.set (' py1 ', ' GJ ') # output response results, Returns True if add succeeds, otherwise returns False print (result) Except exception as e: print (e)- string-get from redis import *if __name__ == "__main__": try: # Create Strictredis object, link with Redis server sr = strictredis () # Gets the value of the key py1 result = sr.get (' py1 ') # the value of the output key,Returns None print (Result) If the key does not exist except Exception as e: print (e)- string-modification from redis import *if __name__ == "__main__": try: # Create a Strictredis object and link to a Redis server sr = strictredis () # set the value of Py1, If the key already exists, modify it and add result = sr.set if the key does not exist (' py1 ', ' HR ') # outputs the corresponding result, returns True if the operation succeeds, or returns false print (Result) except Exception as e: print (e)- string-Delete from redis import *if __ name__ == "__main__": try: # Create Strictredis objects, Establish a link to a Redis server sr = strictredis () # sets the value of the key py1, modifies if the key already exists, and adds it if the key does not exist result = sr.delete (' py1 ') # outputs the corresponding results, Returns the number of keys affected if the deletion succeeds, or returns 0 print (result) Except exception as e: print (e)- get key from redis import *if __name__ == "__main__": try: # Create a Strictredis object and link to a Redis server sr = strictredis () # get all the keys &Nbsp;result = sr.keys () # output response result, all keys form a list, Returns an empty list if no key points print (result) except Exception as e: print (e)
Redis Database--python interaction