c++ mysql 通过字段取数据

c++ 怎么才能通过字段名字取数据.

第1个回答  2008-03-15
这是我以前写的一段代码,希望对你有帮助
typedef struct _waa_pageinfo waa_pageinfo;
struct _waa_pageinfo {
unsigned int page_id;
unsigned int chunk_id;
unsigned int start_pos;
unsigned int length;
unsigned int old_max_kid;
/* page_type
** 1: 普通网页 2: 新闻网页 3: BBS 4: BLOG
*/
int page_type;
/* page_state
** 0: 未分析的网页 1: 更新的网页 2: 删除的网页 100: 已分析的网页
**10 11 12 1100 (临时状态)
*/
int page_state;
};

int count = 0;

int record_num;
MYSQL_RES *res = NULL;
MYSQL_ROW row = NULL;

char str_sql[512];
char tmp_sql[128];
if (page_type>=0 && page_state>=0) {
sprintf(str_sql, "select page_id,chunk_id,start_pos,length,page_type,page_state from pageinfo_%d where ", index);
sprintf(tmp_sql, "page_id>=%u and page_type=%d and page_state=%d and chunk_id<>0 limit 1000",
min_pid, page_type, page_state);
strcat(str_sql, tmp_sql);
} else {
sprintf(str_sql, "select page_id,chunk_id,start_pos,length,page_type,page_state from pageinfo_%d where ", index);
sprintf(tmp_sql, "page_id>=%u and chunk_id<>0 limit 1000", min_pid);
}

if (mysql_real_query(mysql, str_sql, strlen(str_sql)) != 0) {
fprintf(stdout, "mysql_real_query() failed at line %d in %s\n", __LINE__, __FILE__);
fprintf(stdout, "%s\n", mysql_error(mysql));
fprintf(stdout, "sql: %s\n", str_sql);
return -1;
}
res = mysql_store_result(mysql);

record_num = mysql_num_rows(res);
if (record_num <= 0) {
mysql_free_result(res);
return -1;
}
if (((*pageinfo)=(waa_pageinfo *)calloc(record_num, sizeof(waa_pageinfo))) == NULL) {
fprintf(stdout, "calloc failed at line %d in %s\n", __LINE__, __FILE__);
mysql_free_result(res);
return -2;
}
while ((row=mysql_fetch_row(res)) != NULL) {
if (row[0]) (*pageinfo)[count].page_id = strtoul(row[0], NULL, 0);
if (row[1]) (*pageinfo)[count].chunk_id = strtoul(row[1], NULL, 0);
if (row[2]) (*pageinfo)[count].start_pos = strtoul(row[2], NULL, 0);
if (row[3]) (*pageinfo)[count].length = strtoul(row[3], NULL, 0);
(*pageinfo)[count].old_max_kid = 0;
if (row[4]) (*pageinfo)[count].page_type = atoi(row[4]);
if (row[5]) (*pageinfo)[count].page_state = atoi(row[5]);
count++;
}
mysql_free_result(res);本回答被提问者采纳