#include <iostream>
#include "hiredis/hiredis.h"
using namespace std;
int main()
{
redisContext *pContext = redisConnect("127.0.0.1",6379); //建立连接
redisReply *pReply;
if(pContext->err)
{
redisFree(pContext);
cout << "connect to redisServer fail" << endl;
return -1;
}
cout << "connect to redisServer success" << endl;
//执行命令
string str= "f1";
pReply = (redisReply*)redisCommand(pContext,"hset myhash f1 a");
pReply = (redisReply*)redisCommand(pContext,"hget myhash %s",str.c_str());
cout<<pReply->str<<endl;
//释放连接
freeReplyObject(pReply);
redisFree(pContext);
return 0;
}
运行结果
把
string str= "f1";
pReply = (redisReply*)redisCommand(pContext,"hset myhash f1 a");
pReply = (redisReply*)redisCommand(pContext,"hget myhash %s",str.c_str());
换成
string str= "myhash f1";
pReply = (redisReply*)redisCommand(pContext,"hset myhash f1 a");
pReply = (redisReply*)redisCommand(pContext,"hget %s",str.c_str());
报错
证明一个字符串str只能有一个参数