soubhagya
soubhagya

Reputation: 806

how can i check if value is exist in redis-database using python-redis

Here is my python code:

def check(request):
    import redis
    R = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
    R.set("name","srk")
    r = R.HSET("name","srk")
    print(r)

It encounters an error with this error message:

Internal Server Error: /get_user/
Traceback (most recent call last):
  File "/home/soubhagya/.local/share/virtualenvs/pipenv-r-zifbiy/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/soubhagya/.local/share/virtualenvs/pipenv-r-zifbiy/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/soubhagya/.local/share/virtualenvs/pipenv-r-zifbiy/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/soubhagya/Desktop/Dev/rango/backend/access/views.py", line 103, in get_user
    r = R.HSET("name","srk")
AttributeError: 'StrictRedis' object has no attribute 'HSET'

In py-redis, how can I check if a value exists or not in redis database. A raw query will be helpful. Please have a look into my code.

Upvotes: 3

Views: 8878

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50122

StrictRedis has no HSET function, but it does have the hset function for setting fields in Redis Hashes. That is the cause for the error you're getting.

To check whether a key exists in Redis with redis-py, use r = R.exists("name").

Upvotes: 5

Related Questions