如何在hiredis中使用SADD命令?

时间:2016-07-17 12:55:54

标签: c++ c database redis hiredis

我想在C中将数据插入redis。我找到了hiredis库。

我写了一个例子:

redisContext *c = redisConnect("127.0.0.1", 6379);
if (c != NULL && c->err)
{
    printf("Error: %s\n", c->errstr);
    // handle error
} else
{
    printf("Connected to Redis\n");
}

redisReply *reply;
reply = (redisReply *)redisCommand(c, "AUTH 123456");


if(reply->type==5)
{ 
    reply = (redisReply *)redisCommand(c,"SET %d %d",32,111);
    freeReplyObject(reply);

    reply = (redisReply *)redisCommand(c,"GET %d",32);
    printf("%s\n",reply->str);

    int ii = redisAppendCommand(c,"SADD %d %d",32,33);// MY PROBLEM IS HERE 
    printf("-------SADD---------------- %d\n",ii);

我不知道如何使用SADD命令。求你帮帮我。

1 个答案:

答案 0 :(得分:0)

正确答案是

redisReply *rreply;
char buffer[4096];
sprintf(buffer,"%u,%u,%u,%u,%s,%u,%d", 1,2,3,4,"HI",5,6);
redisAppendCommand(c,"SADD  %s %s","slog1",buffer);
redisGetReply(c,(void**)rreply);

关键是我必须使用这个redisAppendCommand命令。 第二个键是redisAppendCommand命令,只有在此命令后才将其插入缓冲区我必须使用此redisGetReply命令使其永久化。