C language calls to MySQL stored procedures

Source: Internet
Author: User

This article explains the C language method of invoking the stored procedure of MySQL database

The following assumes that there is a SC table, save the student selection record, there is a course number, school number, peacetime points, scaled points, total score.
To establish a database table procedure:
CREATE TABLE Class (
CNO varchar (8) NOT NULL,
Sno varchar (8) NOT NULL,
Ordinary_score int,
Last_score int,
All_score int
);

Stored Procedures
The parameter column surrounded by parentheses must always exist. If there are no parameters, you should also use an empty parameter column (). Each parameter is an in parameter by default. To be specified as a different parameter, use the keyword in (default, which can default) out or InOut before the parameter name.
In parameter is only passed in
Out parameter is only outgoing
The InOut parameter is both passed in and passed in, i.e. two-way delivery

Specifying that the parameter is in, out, or inout is only valid for procedure. (the function parameter is always considered an in parameter)

Set up stored procedures, passed in peacetime X, scaled sub-y, the proportion of the usual percentage of pert, school number, course number; the establishment process is as follows

Delimiter//CREATE  PROCEDURE cal_grade(x int,y INT, out T int,pert float,s VARCHAR (8),c VARCHAR (8)) Label_proc:BEGIN  IF(X <0|| X > -) Then      SETt =-1; LEAVE Label_proc;END IF;IF(Y <0|| Y > -) Then       SETt =-2; LEAVE Label_proc;END IF;SETt = ROUND (X*pert + y* (1-pert)); UPDATE SCSETOrdinary_score=x,last_score=yWHERESno=s andCno=c andTno=tn;ENDLabel_proc//delimiter;

C Language Call

#include <stdio.h>#include "mysql.h"intMain () {MYSQL*my_connection; Mysql_res*res_ptr;    Mysql_row Sqlrow; Char buf[ -]; My_connection = Mysql_init (NULL);//The last parameter of the connection below must be client_multi_statements, or it will be an error.SelectError:procedure*** can ' treturnA result set in thegivenContext my_connection = Mysql_real_connect (My_connection,"localhost","Root","Root","Test",0, NULL, client_multi_statements);sprintf(BUF,"Call Cal_grade (%d,%d,@t,%f,%s,%s)",Ten,Ten,0.3,123,456);if(mysql_query (My_connection, buf))sprintf(stderr, Mysql_error (my_connection));Else{//Get Return parameters@t,@tIs the outgoing parameter mysql_query (my_connection,"Select @t"); Res_ptr = Mysql_store_result (my_connection);if(RES_PTR) {Sqlrow = mysql_fetch_row (res_ptr);if(!STRCMP (sqlrow[0],"-1"))printf("It's not in the range \ nthe usual.");Else if(!STRCMP (sqlrow[0],"-2"))printf("Scaled is not within range \ n");Else               printf("Total divided into:%s\ n", sqlrow[0]);    } mysql_free_result (RES_PTR); } mysql_close (My_connection);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C language calls to MySQL stored procedures

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.