c++ mysql 通过字段取数据

c++怎么才能通过字段名字取数据.... c++ 怎么才能通过字段名字取数据. 展开
 我来答
alberte_peng
2008-03-15 · TA获得超过132个赞
知道小有建树答主
回答量:222
采纳率:50%
帮助的人:107万
展开全部
这是我以前写的一段代码,希望对你有帮助
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);
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式