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

数据库评论520字数 244阅读模式

通常情况MySQL出于安全考虑不允许一次执行多条语句(但也不报错,很让人郁闷)。

MySQL是支持在单个查询字符串中指定多语句执行的,使用方法是给链接指定参数:很文博客-https://www.zhenjiekeji.com/50338.html

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

当使用执行多语句功能后,一定要读完整个resault集,否则会出现错误:Commands out of sync; you can't run this command now很文博客-https://www.zhenjiekeji.com/50338.html

官方推荐的执行语句是这样的:很文博客-https://www.zhenjiekeji.com/50338.html

  1. do
  2. {
  3. /* Process all results */
  4. ...
  5. printf( "total affected rows: %lld", mysql_affected_rows( mysql ) );
  6. ...
  7. if( !( result mysql_store_result( mysql ) ) )
  8. {
  9. printf( stderr, "Got fatal error processing query\n" );
  10. exit(1);
  11. }
  12. process_result_set(result); /* client function */
  13. mysql_free_result(result);
  14. }while( !mysql_next_result( mysql ) );

如果仅仅是插入等不需要返回值的SQL语句,也一样得读完整个resault集并释放,最小化的写法:很文博客-https://www.zhenjiekeji.com/50338.html

  1. do
  2. {
  3. result = mysql_store_result( mysql );
  4. mysql_free_result(result);
  5. }while( !mysql_next_result( mysql ) );
很文博客-https://www.zhenjiekeji.com/50338.html很文博客-https://www.zhenjiekeji.com/50338.html
weinxin
我的微信
微信号已复制
扫一扫更精彩
大家的支持是我更新的动力!!!
匿名

发表评论

匿名网友