[英]Sqlite3 and C, Populate a structure with the callback
我對使用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);
我有段錯誤
在此先感謝您,對於使用該庫的人來說可能很容易,但是我不是,而且我發現的每個教程都不使用回調的第一個參數
祝你今天愉快
我不知道那個特定的API,但是
pHost = &host;
您正在返回局部變量的地址。 呼叫返回后銷毀。 您可能想要的是
* (struct host *) pHost = host
因為這已經是一個指針。
你好,答案是關於指針和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;
}
您的原型和作業不正確:
static int buildHost(struct host**pHost, int argc, char **argv, char **azColName) {
...
*pHost = host;
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.