0

Well ,I am using hiredis client in centos6 to connect redis server and using it's redisAppendCommand() to send command to the server.

redisContext *redisConnect(ip,port);
std::string value = "E 1";
std::string field_name = "field";
std::string id_code = "id";
std::string key = "HSET type:info:"+ id_code + " " + field_name + " " +value;

redisAppendCommand(_contxt, key.c_str());

It could not set value to E 1 as I wanted. Then I change code like this,

redisContext *redisConnect(ip,port);
std::string value = "E 1";
std::string field_name = "field";
std::string id_code = "id";
std::string key = "HSET type:info:"+ id_code + " " + field_name + " \"" +value + "\"";
redisAppendCommand(_contxt, key.c_str());

But the value would contain \" as it's content ,so value becomes \"E 1\", I just want to know if there any way to set value just E 1 with hiredis? Thank you.

MordechayS
  • 1,552
  • 2
  • 19
  • 29
Joe.Q
  • 1
  • 2

1 Answers1

0

You can use the format string to specify the command arguments:

string key_name = "type:info:" + id_code;
redisAppendCommand(redisContext,
    "%s %b %b %b",
    "HSET",
    key_name.data(), key_name.size(),
    field_name.data(), field_name.size(),
    value.data(), value.size());

%b, in the format string, means binary string. With this flag, you can specify any characters for key name, field name and value. Since it's binary string, you have to specify the length of the string.

for_stack
  • 21,012
  • 4
  • 35
  • 48
  • I think you want just `redisAppendCommand`. `redisvAppendCommand` is for passing through an existing va_list. – nnog Nov 02 '16 at 10:10