1. 程式人生 > >【Redis】6.Redis資料型別 雜湊的基本使用

【Redis】6.Redis資料型別 雜湊的基本使用

#!/usr/bin/python
#!coding: utf-8
import redis

if __name__=="__main__":
    try:
        conn=redis.StrictRedis(host='127.0.0.1',port=6379,db=0)
        print(conn.ping())
        # 輸出 True
        # 讓hash表persons:001的name關鍵字(key)關聯到一個值‘jiangle’
conn.hset('persons:001','name','123')
        # 取出hash表persons:001的name關鍵字所關聯的值
name=conn.hget('persons:001','name') print(name) # 輸出 b'123' # 取得hash表persons:001中所有的鍵值 print(conn.hgetall('persons:001')) # 輸出 {b'name': b'123'} # 判斷hash表persons:001是否存在name這個鍵 print(conn.hexists('persons:001','name')) # 輸出 True print(conn.hget('persons:001','age'
)) # 輸出 None # 自增 如果原來是1的話,經過hincrby方法之後會變成3,如果原來沒有的話,預設是從0開始。 conn.hincrby('persons:001','age',2) print(conn.hget('persons:001','age')) # 輸出 b'2' # 刪除鍵 conn.hdel('persons:001','age') print(conn.hkeys('persons:001')) # 輸出 [b'name'] except Exception as
err: print(err)