#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "/usr/include/mysql/mysql.h"
#include "/usr/include/mysql/mysql_version.h"
#include "/usr/include/mysql/errmsg.h"
int main( int argc,char **argv[] )
{
MYSQL my_connection;
MYSQL_RES *results;
MYSQL_ROW record;
MYSQL_FIELD *field;
int row, column,i,j;
/** 链接数据库 **/
if(!mysql_init(&my_connection))
{
printf("mysql_init failed!/n");
return 0;
}
if(!mysql_real_connect(&my_connection,"localhost","root","123456","yyw",0,NULL,CLIENT_MULTI_STATEMENTS))
{
printf("mysql_real_connect() failed!/n");
mysql_close(&my_connection);
return 0;
}
/*这句话是设置查询编码为utf8,这样支持中文*/
mysql_query(&my_connection, "set names utf8");
/** 调用存储过程p1**/
mysql_real_query(&my_connection, "call p1(11)", (unsigned int)strlen("call p1(11)"));
/** 取得存储过程返回值 **/
results = mysql_store_result(&my_connection);
if (results)
{
/*取得結果的行数和*/
column = mysql_num_fields(results);
row = mysql_num_rows(results) + 1;
printf("查询到 %lu 行/n", row);
/*输出結果的字段名*/
for (i = 0; field = mysql_fetch_field(results); i++)
printf("%s/t", field->name);
printf("/n");
/*按行输出結果*/
for (i = 1; i < row; i++)
{
record = mysql_fetch_row(results);
for (j = 0; j < column; j++)
printf("%s/t", record[j]);
printf("/n");
}
}
mysql_free_result(results);
mysql_close(&my_connection);
return 0;
}
编辑一下:
[root@localhost home]# gcc -o test test.c -L/usr/lib/mysql/ -lmysqlclient -lz
执行:
[root@localhost home]# ./test
显示:
查询到 10 行
NUM Name Enlish Maths Physis Total Aver
1 杨业 92 87 96 275 91.67
2 剑锋 82 98 93 273 91.00
3 张美 96 86 94 276 92.00
4 张文 76 99 95 270 90.00
5 叶倩 97 86 88 271 90.33
6 方文 87 96 94 277 92.33
7 李丽 97 86 83 266 88.67
8 贾宇 67 89 77 233 77.67
9 王勃 89 67 75 231 77.00
10 刘三 85 78 95 258 86.00