#include stdio.h#include hiredis/hiredis.hint main(){redisContext *c = redisConnect(127.0.0.1, 6379);if (c == NULL || c-err) {if (c) {printf(Error: %s\n, c-errstr);// handle error} else {...
#include <stdio.h>
#include <hiredis/hiredis.h>
int main()
{
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c == NULL || c->err) {
if (c) {
printf("Error: %s\n", c->errstr);
// handle error
} else {
printf("Can't allocate redis context\n");
}
}
redisReply *reply=NULL;
// set
reply = redisCommand(c, "set %s %s", "name", "程劲");
printf("set is %s\n", reply->str);
freeReplyObject(reply);
// get
reply = redisCommand(c, "get name");
printf("name :%s\n", reply->str);
freeReplyObject(reply);
printf("Hello World!\n");
return 0;
}
运行结果:

前提:在github上找到 hiredis库,下载下来。在解压的文件中运行 make make install 完成libhireids.so的生成。
作为85后,习惯了在windows系统中,下个exe文件,双击安装,然后就可以在visual studio名正言顺的按照,某一款数据库或平台系统官方文档提供的示范代码,根据业务需求开发,现如今移到linux上,在终端上运行命令安装扩展库,还真是大开眼界
时下更流行的做法是某些平台或数据库的连接代码和API全都放在github上,既可以方便维护升级,也方便分享。不过总给人感觉很野路子,可能是我太保守了。这里使用了hiredis这一连接库,当然别忘了在Qt项目中,添加库libhiredis.so
本文标题为:一例C语言凭借hiredis连接redis
基础教程推荐
- 漫画讲解C语言中最近公共祖先的三种类型 2023-01-01
- 使用C/C++读写.mat文件的方法详解 2023-03-05
- 如何告诉 MinGW 链接器不要导出所有符号? 2022-10-07
- C语言文件操作与相关函数介绍 2023-06-13
- C/C++ Qt StatusBar底部状态栏应用教程 2023-01-10
- 使用VS2022开发在线远程编译部署的C++程序(图文详解) 2023-01-15
- C++高级数据结构之并查集 2023-04-20
- C++类和对象到底是什么 2022-11-12
- C语言预编译#define(预处理) 2023-04-03
- C语言实现简易停车场管理系统 2023-03-13
