Home >Database >Mysql Tutorial >MySQL 一次执行多条语句的实现及常见问题_MySQL

MySQL 一次执行多条语句的实现及常见问题_MySQL

WBOY
WBOYOriginal
2016-06-01 13:19:341136browse

bitsCN.com MySQL是支持在单个查询字符串中指定多语句执行的,使用方法是给链接指定参数:

//链接时设定
mysql_real_connect( ..., CLIENT_MULTI_STATEMENTS );
//或者
//中途指定
mysql_set_server_option( mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON ); //mysql是连接的名称

当使用执行多语句功能后,一定要读完整个resault集,否则会出现错误:Commands out of sync; you can't run this command now
官方推荐的执行语句是这样的:

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 ) );

如果仅仅是插入等不需要返回值的SQL语句,也一样得读完整个resault集并释放,最小化的写法:

do
{
    result = mysql_store_result( mysql );
    mysql_free_result(result);
}while( !mysql_next_result( mysql ) );
bitsCN.com

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn