C调用存储过程

#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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值