使用c语言操作MYSQL数据库时可以编译,也可以正常运行,就是显示表内容之后就有提示段错误,求指导~~
#include<mysql/mysql.h>#include<stdio.h>#include<string.h>voidmain(void){MYSQL*mysql;...
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
void main(void)
{
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_FIELD *fields;
const char *query = "select * from data_import order by id";
int num_row = 0;
int num_col = 0;
int r;
int c;
mysql = mysql_init(NULL);
if(mysql_real_connect(mysql,"localhost","root","123","test",0,NULL,0) == NULL)
printf("Erro connecting to database: %s\n",mysql_error(mysql));
else
printf("connected..\n");
if(mysql_real_query(mysql,query,strlen(query)))
printf("Error making query: %s\n",mysql_error(mysql));
else
{
printf("Query make..\n");
res = mysql_store_result(mysql);
num_row = (int)mysql_num_rows(res);
num_col = (int)mysql_num_fields(res);
for(r=0;r<num_col;r++)
{
row = mysql_fetch_row(res);//检索一个结果集合的下一行
for(c=0;c<num_col;c++)
printf("%s ",row[c]);
printf("\n");
}
mysql_free_result(res);
}
mysql_close(mysql);
}
运行结果如下:
connected..
Query make..
1 Tom 24 beijing football
2 Liu 27 guangdong football
3 Jim 23 shanghai football
4 Han 25 shanxi football
段错误
注:是在linux平台的 展开
#include <stdio.h>
#include <string.h>
void main(void)
{
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_FIELD *fields;
const char *query = "select * from data_import order by id";
int num_row = 0;
int num_col = 0;
int r;
int c;
mysql = mysql_init(NULL);
if(mysql_real_connect(mysql,"localhost","root","123","test",0,NULL,0) == NULL)
printf("Erro connecting to database: %s\n",mysql_error(mysql));
else
printf("connected..\n");
if(mysql_real_query(mysql,query,strlen(query)))
printf("Error making query: %s\n",mysql_error(mysql));
else
{
printf("Query make..\n");
res = mysql_store_result(mysql);
num_row = (int)mysql_num_rows(res);
num_col = (int)mysql_num_fields(res);
for(r=0;r<num_col;r++)
{
row = mysql_fetch_row(res);//检索一个结果集合的下一行
for(c=0;c<num_col;c++)
printf("%s ",row[c]);
printf("\n");
}
mysql_free_result(res);
}
mysql_close(mysql);
}
运行结果如下:
connected..
Query make..
1 Tom 24 beijing football
2 Liu 27 guangdong football
3 Jim 23 shanghai football
4 Han 25 shanxi football
段错误
注:是在linux平台的 展开
2个回答
推荐于2017-09-20
展开全部
段错误一般是指针有问题,或者越界之类的
num_row = (int)mysql_num_rows(res);
num_col = (int)mysql_num_fields(res);
for(r=0;r<num_col;r++)
{
row = mysql_fetch_row(res);//检索一个结果集合的下一行
for(c=0;c<num_col;c++)
printf("%s ",row[c]);
printf("\n");
}
这段代码很可疑啊
num_row = (int)mysql_num_rows(res);
num_col = (int)mysql_num_fields(res);
for(r=0;r<num_col;r++)
{
row = mysql_fetch_row(res);//检索一个结果集合的下一行
for(c=0;c<num_col;c++)
printf("%s ",row[c]);
printf("\n");
}
这段代码很可疑啊
追问
发现问题了,有点笔误for(r=0;r<num_row;r++)写错了,导致段错误,请问有没有写过MYSQL的C_API呢?my_use_result()跟my_store_result()用法有什么区别,为什么同样的程序用mysql_store_result()可以,而用mysql_use_result()就没有显示列表内容?
追答
不好意思,我也木研究过。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询