C call the Stored Procedure

Source: Internet
Author: User

# Include "stdio. H"

# Include "stdlib. H"

# Include "string. H"

# Include "/usr/include/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;

/** Link database **/

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;

}

 

/* Set the query encoding to utf8 to support Chinese characters */

Mysql_query (& my_connection, "set names utf8 ");

/** Call the Stored Procedure P1 **/

Mysql_real_query (& my_connection, "Call p1 (11)", (unsigned INT) strlen ("Call p1 (11 )"));

/** Get the Stored Procedure return value **/

Results = mysql_store_result (& my_connection );

If (results)

{

/* The number of rows and */

Column = mysql_num_fields (results );

Row = mysql_num_rows (results) + 1;

Printf ("% lu rows/N", row );

/* Field name of the output result */

For (I = 0; field = mysql_fetch_field (results); I ++)

Printf ("% S/T", field-> name );

Printf ("/N ");

/* Result output by line */

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;

}

Edit:

[Root @ localhost Home] # gcc-O test. C-L/usr/lib/MySQL/-lmysqlclient-LZ

Run:

[Root @ localhost Home] #./test

Display:

10 rows are queried.
Num name enlish maths physis total aver
1 Yang Ye 92 87 96 275 91.67
2 Jianfeng 82 98 93 273 91.00
3 Zhang Mei 96 86 94 276 92.00
4 Zhang wen76 99 95 270 90.00
5 ye Qian 97 86 88 271 90.33
6. Fang Wen 87 96 94 277 92.33
7 Li 97 86 83 266 88.67
8 Jia Yu 67 89 77 233 77.67
9 Wang Bo 89 67 75 231 77.00
10 Liu San 85 78 95 258 86.00

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.