redis数据库拥有方便快捷的c语言接口,下面我将用程序操作redis数据库。
首先redis的c语言接口API是hiredis,下载地址为https://github.com/redis/hiredis
将此API下载到本地:
git clone https://github.com/redis/hiredis编译安装: make make install下面是c语言接口代码redisrun.c
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <stdarg.h> #include <string.h> #include <assert.h> #include <memory.h> #include <hiredis/hiredis.h> void RedisGet(char* key){ redisContext* c = redisConnect("192.168.3.242", 7000); if(c->err){ redisFree(c); printf("Connect to redisServer faile\n"); return ; } printf("Connect to redisServer Success, ready to Get\n"); char command[100] = "get "; strcat(command, key); redisReply* r = (redisReply*)redisCommand(c, command); if ( r->type != REDIS_REPLY_STRING){ printf("Failed to execute command[%s]\n",command); freeReplyObject(r); redisFree(c); return; } printf("The value of '%s' is %s\n", key, r->str); freeReplyObject(r); redisFree(c); } void RedisPut(char* key, char* value){ redisContext* c = redisConnect("192.168.3.242", 7000); if(c->err){ redisFree(c); printf("Connect to redisServer faile\n"); return ; } printf("Connect to redisServer Success, ready to Put\n"); char command[1000] = "set "; strcat(command, key); strcat(command," "); strcat(command, value); redisReply* r = (redisReply*)redisCommand(c, command); if(NULL == r){ printf("Execut command failure\n"); redisFree(c); return; } if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0)){ printf("Failed to execute command[%s]\n",command); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command); redisFree(c); } struct SFCOrder{ int SFCid; int SFCNumber; int SFClist[10]; }; int struct_to_str(char **dest, struct SFCOrder *pHead) { unsigned int size = 0; if (dest == NULL || pHead == NULL) return -1; size = 128 + 13 * sizeof(int) ; *dest = (char *)malloc(size); if (*dest == NULL) return -2; memset(*dest, 0, size); sprintf(*dest, "SFClist[1]=%d," "SFClist[2]=%d," "SFClist[3]=%d," "SFClist[4]=%d," "SFClist[5]=%d" return 0; } int str_to_struct(char *str, struct SFCOrder **pHead) { if (str == NULL || pHead == NULL) return -1; *pHead = (struct SFCOrder *)malloc(sizeof(struct SFCOrder)); if (*pHead == NULL) return -2; memset(*pHead, 0, sizeof(struct SFCOrder)); sscanf(str, "SFCid=%d," "SFCNumber=%d," "SFClist[1]=%d," "SFClist[2]=%d," "SFClist[3]=%d," "SFClist[4]=%d," "SFClist[5]=%d" return 0; } int main(int argc, char *argv[]) { struct SFCOrder head_a; struct SFCOrder *p = NULL; char *str = NULL; int res; head_a.SFCid = 6; head_a.SFCNumber = 4; head_a.SFClist[1] = 2; head_a.SFClist[2] = 3; head_a.SFClist[3] = 4; head_a.SFClist[4] = 0; head_a.SFClist[5] = 0; res = struct_to_str(&str, &head_a); if (res != 0) { printf("Failed to execute struct_to_str"); return -1; } char* key = "SFC6"; char* value = str; RedisPut(key, value); RedisGet(key); res = str_to_struct(str, &p); if (res != 0) { free(str); str = NULL; } free(p); p = NULL; printf("succuss\n"); return 0; }-
编译上面代码: gcc redisrun.c -o redisrun -lhiredis运行代码: ./redisrun这时会遇到下面问题: ./redis: error while loading shared libraries: libhiredis.so.0.13: cannot open shared object file: No such file or directory这是因为系统找不到动态库的问题,需要检查/etc/ld.so.conf文件中是否包含需要的动态库目录(如/usr/local/lib),如果没有进行添加。之后执行sudo /sbin/ldconfig,更新系统动态库配置此时在运行程序即可,显示:
redis数据库c语言接口测试完毕