python操作redis-hash

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

import redis



if __name__=="__main__":
    try:
        conn=redis.StrictRedis(host='192.168.80.128',port=6379,db=0)
        print(conn.ping())

        conn.hset('persons:001','name','jiangle')
            #让hash表persons:001的name关键字(key)关联到一个值‘jiangle’

        name=conn.hget('persons:001','name')
        print(name)
            #取出hash表persons:001的name关键字所关联的值

        print(conn.hgetall('persons:001'))
            #取得hash表persons:001中所有的键值

        print(conn.hexists('persons:001','name'))
            #判断hash表persons:001是否存在name这个键

        print(conn.hget('persons:001','age'))
        conn.hincrby('persons:001','age',2)
        print(conn.hget('persons:001','age'))
            #自增

        conn.hdel('persons:001','age')
        print(conn.hkeys('persons:001'))
            #删除键
    except Exception as err:
        print(err)

 

posted on 2016-04-18 09:40  蒋乐兴的技术随笔  阅读(514)  评论(0编辑  收藏  举报

导航