C + + calls MySQL stored procedure

Source: Internet
Author: User
Tags error code printf mysql database

E-Text Good please check the original, rookie-level translation, welcome to criticize correct!
mysql5.0 support for stored procedures, the Mysql6.0alpha version is now available. 6.0 not only supports large databases such as Oracle and most features such as stored procedures, views, triggers, jobs, and so on, and fixes the bug,6.0.1 version of these features also supports Windows 64-bit and tablespace.
C/C + + to access MySQL, just a few simple common operations, the simple use of the "embedded", insert, update and other operations can be. These statements as a business complex, complete business functions required by SQL operations, a large part of the embedded C code C code will lead to more and more complex unclear this time the natural stored procedures to encapsulate all the logic of the database, simply by invoking the MySQL stored process c Interfaces can achieve their goal of significantly reducing the workload of C programmers, but also easy to handle front-end business logic and database processing logic separation. Here you only invoke the stored Procedure C language simple method.

1, first create a table to store user information

?
1 2 3 4 5 6 7 Create table student (id int auto_increment, name varchar (), age tinyint, remark varchar (50), Primary key (ID));

2, insert a few user information

?
1 2 Insert into student values (1, "Zhouys", 90, ""); Commit

3, view the user information just inserted

?
1 2 3 4 5 6 7 8 9 Mysql> select * from student; +------+-----------+------+----------+ | ID | name | Age | Remark |    +------+-----------+------+----------+ | 1 |   Zhouys |        90 | | +------+-----------+------+-----------+ 1 row in Set (0.00 sec) mysql>

4. Create a stored procedure

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28 delimiter//   CREATE PROCEDURE querystudent (    & nbsp;  in in_id int,   #0-character ID 1-digital id    nbsp;  #        out Out_ret int,                 # Returns The result         out out_name varchar,        # name         out out_age   int                 # age    ) Label_a:begin         DECLARE v_name varchar (20);        declare v_age tinyint;        # Parameters determine        if (in_id<=0) then               set Out_ret =-1; #id error               leave label_a;        end if;                        select name,age into v_name,v_age from student where ' t found                leave label_a;        end if;                           set out_ret=0;        set Out_name=v_name;        set Out_age=v_age; End delimiter;

5, C language to call the stored procedure method steps:

5.1, the Mysql handle initialization

?
1 2 3 4 5 if (!mysql_init (&mysql)) {printf ("Mysql_init failed!n");        return 0; }

5.2. Connect to MySQL Database

?
1 2 3 4 5 6 7 Login or Connect if (!mysql_real_connect (&mysql, "localhost", "root", "", "Billingdb", 0,null,client_multi_               statements)) {printf ("Mysql_real_connect () failed!n");               Mysql_close (&mysql);        return 0; }

5.3. Calling stored Procedures

?
1 2 3 4 Call strcpy (query, "Call Querystudent (1, @ret, @ out_name,@ out_age)");     printf ("Query Sql=[%s]n", query); Ret= Mysql_real_query (&mysql,query, (unsigned int) strlen (query);

5.4. Query return value and output parameter value

?
1 2 3 4 5 6 7 8 9 10 11 12-13 mysql_query (&mysql, "select @ret, @ out_name,@ out_age");     Get result if (ret) {printf ("Error exec query:%sn", Mysql_error (&mysql));     else {printf ("[%s] EXEC...N", query); } results = Mysql_store_result (&mysql);

5.5. Get the result value

?
1 2 3 while (record = mysql_fetch_row (results)) {printf ("[%s]-[%s]-[%s]n", record[0], record[1],record[2]); }

Regular stored procedures will only return results, ^_^.

5.6, release resources and MySQL connection handle

?
1 2 Mysql_free_result (results); Mysql_close (&mysql);

6. Conclusion

Mysql stored procedures can achieve quite powerful features, here are just some basic usage, I hope to give you some help. Also welcome to share ...

7. Experience of personal use

A C # call can bind an output parameter and get a value directly from the output parameter, but the prepare binding output parameter is used in C + +, but the following error message is returned:

Error code:1414. Out or INOUT argument 2 for routine knockout_dbo.ko_signupyoungboxer be not a variable or NEW pseudo-variable in before TR Igger

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.