0

我想在 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 命令。请帮我。

4

1 回答 1

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 命令使其永久化。

于 2016-07-19T06:00:09.463 回答