Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to select different db execution "keys *" #1120

Closed
YuanlvCold opened this issue Sep 23, 2022 · 4 comments
Closed

How to select different db execution "keys *" #1120

YuanlvCold opened this issue Sep 23, 2022 · 4 comments

Comments

@YuanlvCold
Copy link

YuanlvCold commented Sep 23, 2022

redisReply * _reply;

 _reply = (redisReply*)redisCommand(_redisCxt, "select 1");
 _reply = (redisReply*)redisCommand(_redisCxt, "keys *");

if (_reply->type == REDIS_REPLY_ARRAY){
        for (int i = 0; i < _reply->elements; ++i){
             _reply->element[i]->str;
        }
}

question:
The keys I got are all in the database db0.
I need to select different db to get all keys at the same time, what do I need to do. thanks!

@bjosv
Copy link
Contributor

bjosv commented Sep 23, 2022

Using select 1 is the correct way to tell Redis that the client wants to use logical db 1.
Either the Redis instance has been setup as a cluster node (select is not supported in cluster mode) or there are other errors.
One suggestion in to make sure the first redisCommand(select 1) results in a non-null reply, and is of type REDIS_REPLY_STATUS and str is OK.

@michael-grunder
Copy link
Collaborator

As @bjosv says, you should check to make sure the SELECT command is not returning an error.

#include <hiredis/hiredis.h>
#include <string.h>
#include <assert.h>

#include <hiredis/hiredis.h>
#include <string.h>
#include <assert.h>

int main(void) {
    redisContext *_redisCtx;
    redisReply *_reply;

    _redisCtx = (redisContext*)redisConnect("localhost", 6379);
    assert(_redisCtx != NULL && _redisCtx->err == 0);

    _reply = (redisReply*)redisCommand(_redisCtx, "select 1");
    assert(_reply != NULL && _reply->type == REDIS_REPLY_STATUS &&
           _reply->len == 2 && !strcmp(_reply->str, "OK"));
    freeReplyObject(_reply);

    _reply = (redisReply*)redisCommand(_redisCtx, "keys *");
    assert(_reply != NULL && _reply->type == REDIS_REPLY_ARRAY);

    for (size_t i = 0; i < _reply->elements; i++) {
        assert(_reply->element[i]->type == REDIS_REPLY_STRING);
        printf("KEY[%zu]: %s\n", i, _reply->element[i]->str);
    }

    freeReplyObject(_reply);
    redisFree(_redisCtx);
}

Cluster mode being enabled is the most likely issue (as it disables database numbers) but you can also verify this via redis-cli.

I'm going to close this issue since it's unlikely to be a bug in hiredis but I'm happy to help you figure it out. 😄

@YuanlvCold
Copy link
Author

Using select 1 is the correct way to tell Redis that the client wants to use logical db 1. Either the Redis instance has been setup as a cluster node (select is not supported in cluster mode) or there are other errors. One suggestion in to make sure the first redisCommand(select 1) results in a non-null reply, and is of type REDIS_REPLY_STATUS and str is OK.

Your reply solved my problem. Thank you!

@YuanlvCold
Copy link
Author

As @bjosv says, you should check to make sure the SELECT command is not returning an error.

#include <hiredis/hiredis.h>
#include <string.h>
#include <assert.h>

#include <hiredis/hiredis.h>
#include <string.h>
#include <assert.h>

int main(void) {
    redisContext *_redisCtx;
    redisReply *_reply;

    _redisCtx = (redisContext*)redisConnect("localhost", 6379);
    assert(_redisCtx != NULL && _redisCtx->err == 0);

    _reply = (redisReply*)redisCommand(_redisCtx, "select 1");
    assert(_reply != NULL && _reply->type == REDIS_REPLY_STATUS &&
           _reply->len == 2 && !strcmp(_reply->str, "OK"));
    freeReplyObject(_reply);

    _reply = (redisReply*)redisCommand(_redisCtx, "keys *");
    assert(_reply != NULL && _reply->type == REDIS_REPLY_ARRAY);

    for (size_t i = 0; i < _reply->elements; i++) {
        assert(_reply->element[i]->type == REDIS_REPLY_STRING);
        printf("KEY[%zu]: %s\n", i, _reply->element[i]->str);
    }

    freeReplyObject(_reply);
    redisFree(_redisCtx);
}

Cluster mode being enabled is the most likely issue (as it disables database numbers) but you can also verify this via redis-cli.

I'm going to close this issue since it's unlikely to be a bug in hiredis but I'm happy to help you figure it out. 😄

Through the example you gave, I checked my code and found that reply->len==43, when "select %s", a vector parameter value forgot to convert to c_str(). Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants