我使用Sqlite时很安静,我想在exec函数中使用回调来填充结构(使用第4个参数作为结构的指针)
我试过这个:
static int buildHost(void * pHost, int argc, char **argv, char **azColName){
int i;
struct host* host = malloc(sizeof(struct host));
struct snmp_info* inf = malloc(sizeof(struct snmp_info));
host->info = inf;
for(i=0; i<argc; i++){
if(strcmp(azColName[i], "ip") == 0)
host->ip = argv[i] ? argv[i] : NULL;
else if (strcmp(azColName[i], "port") == 0)
host->info->port = argv[i] ? argv[i] : NULL;
else if (strcmp(azColName[i], "community") == 0)
host->info->community = argv[i] ? argv[i] : NULL;
else if (strcmp(azColName[i], "SNMPv") == 0)
host->info->SNMPv = argv[i] ?atoi( argv[i] ) : 0;
else if (strcmp(azColName[i], "auth_protocol") == 0)
host->info->auth_protocol = argv[i] ? argv[i] : NULL;
else if (strcmp(azColName[i], "password") == 0)
host->info->password = argv[i] ? argv[i] : NULL;
else if (strcmp(azColName[i], "encryption") == 0)
host->info->encryption = argv[i] ? argv[i] : NULL;
else if (strcmp(azColName[i], "encryption_passwd") == 0)
host->info->encryption_passwd = argv[i] ? argv[i] : NULL;
}
pHost = &host; //Seems to be the problem line
printf("\n");
return 0;
}
我称这个函数为:
struct host* toRet;
sqlite3_exec(db, request, buildHost, &toRet,0);
我想补充一点,我的代码编译,如果我在回调函数中做了一些printf我有好的数据(我在一个键上做了选择,所以我只能有一个答案或没有) 但是当我尝试在调用函数中执行
时printf("%s", toRet->ip);
我有一个段错误
提前感谢,对于使用该库的人来说可能很容易,但我不是,而且我发现的每个教程都没有使用回调的第一个参数
度过愉快的一天
答案 0 :(得分:1)
我不知道特定的API,但是
pHost = &host;
您要返回局部变量的地址。这在呼叫返回后被销毁。您可能想要的是
* (struct host *) pHost = host
因为这已经是一个指针。
答案 1 :(得分:1)
你好答案是关于指针和CL的mfro的混合。关于新界面 但是最大的错误是我没有分配char *所以他们在电话会议后消失了。
现在,我的两个功能已经纠正了可能属于我的人:
static struct host* buildHost(sqlite3_stmt* stmt){
int i;
struct host* host = malloc(sizeof(struct host));
struct snmp_info* inf = malloc(sizeof(struct snmp_info));
host->info = inf;
for(i=0; i < 9; i++){
char* colName = (char*)sqlite3_column_name(stmt, i);
char* content = (char*)sqlite3_column_text(stmt, i);
char* content2;
int len;
if(content){
len = strlen(content);
content2 = malloc(sizeof(char) * len);
strcpy(content2, content);
}
if(strcmp(colName, "ip") == 0)
host->ip = content ? content2 : NULL;
else if (strcmp(colName, "port") == 0)
host->info->port = content ? content2 : NULL;
else if (strcmp(colName, "community") == 0)
host->info->community = content ? content2 : NULL;
else if (strcmp(colName, "SNMPv") == 0)
host->info->SNMPv = content ? atoi(content) : 0;
else if (strcmp(colName, "auth_protocol") == 0)
host->info->auth_protocol = content ? content2 : NULL;
else if (strcmp(colName, "password") == 0)
host->info->password = content ? content2 : NULL;
else if (strcmp(colName, "encryption") == 0)
host->info->encryption = content ? content2 : NULL;
else if (strcmp(colName, "encryption_passwd") == 0)
host->info->encryption_passwd = content ? content2 : NULL;
}
return host;
}
struct host* getHost(char* ip, sqlite3* db){
sqlite3_stmt *stmt;
char request[1024] = "SELECT * FROM snmp_info WHERE ip= \"" ;
struct host* toRet = malloc(sizeof(struct host));
int rc;
strcat(request, ip);
strcat(request, "\";");
rc = sqlite3_prepare_v2(db, request, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
printf("error: %s!\n", sqlite3_errmsg(db));
return NULL;
}
rc = sqlite3_step(stmt);
if(rc == SQLITE_DONE || rc != SQLITE_ROW)
return NULL;
toRet = buildHost(stmt);
sqlite3_finalize(stmt);
return toRet;
}
答案 2 :(得分:0)
您的原型和作业不正确::
static int buildHost(struct host**pHost, int argc, char **argv, char **azColName) {
...
*pHost = host;
...
}