Implementation of MySQL executing multiple statements at a time and common problems
- 2020-05-09 19:27:51
- OfStack
MySQL supports multiple statement execution in a single query string by specifying parameters to the link:
When using the multi-statement function, 1 must read the entire resault set, otherwise an error will occur: Commands out of sync; you can't run this command now
The official recommended execution statement is this:
If you're just inserting SQL statements that don't need to return a value, you'll have to read the entire resault set and release it.
// Link time setting
mysql_real_connect( ..., CLIENT_MULTI_STATEMENTS );
// or
// Halfway to specify
mysql_set_server_option( mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON ); //mysql Is the name of the connection
When using the multi-statement function, 1 must read the entire resault set, otherwise an error will occur: Commands out of sync; you can't run this command now
The official recommended execution statement is this:
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're just inserting SQL statements that don't need to return a value, you'll have to read the entire resault set and release it.
do
{
result = mysql_store_result( mysql );
mysql_free_result(result);
}while( !mysql_next_result( mysql ) );