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

很文博客hinven.com 数据库评论252字数 749阅读模式
广告也精彩

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

MySQL是支持在单个查询字符串中指定多语句执行的,使用方法是给链接指定参数:文章源自很文博客https://www.hinven.com很文博客-https://www.hinven.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.hinven.com很文博客-https://www.hinven.com/50338.html

官方推荐的执行语句是这样的:文章源自很文博客https://www.hinven.com很文博客-https://www.hinven.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.hinven.com很文博客-https://www.hinven.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.hinven.com很文博客-https://www.hinven.com/50338.html文章源自很文博客https://www.hinven.com很文博客-https://www.hinven.com/50338.html

工具:作品在线观看

女优:最新作品观看

中文:国语在线观看

weinxin
我的微信
扫一扫更精彩
大家的支持是我更新的动力!!!
 
广告也精彩
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证