Implementation and FAQs of MySQL executing multiple statements at a time

Source: Internet
Author: User
Generally, MySQL does not allow execution of multiple statements at a time for security reasons (but no error is reported, which is depressing ).

Generally, MySQL does not allow execution of multiple statements at a time for security reasons (but no error is reported, which is depressing ).

MySQL allows you to specify multiple statements for execution in a single query string by specifying parameters for the link:
The code is as follows:
// Set when linking
Mysql_real_connect (..., CLIENT_MULTI_STATEMENTS );
// Or
// Specify midway through
Mysql_set_server_option (mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); // mysql is the connection name.

When using the multi-statement execution function, you must read the entire resault set; otherwise, an error occurs: Commands out of sync; you can't run this command now
The officially recommended execution statement is as follows:
The code is as follows:
Do
{
/* Process all results */
...
Printf ("total affected rows: % lld", mysql_affected_rows (mysql ));
...
If (! (Result mysql_store_result (mysql )))
{
Printf (stderr, "Got fatal error processing query \ n ");
Exit (1 );
}
Process_result_set (result);/* client function */
Mysql_free_result (result );
} While (! Mysql_next_result (mysql ));

If you just insert an SQL statement that does not need to return a value, you have to read the entire resault set and release it. the minimal syntax is as follows:
The code is as follows:
Do
{
Result = mysql_store_result (mysql );
Mysql_free_result (result );
} While (! Mysql_next_result (mysql ));

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.