mysql error:2014 Commands out of sync; you can't run this command now

最近在项目中经常会出现数据库操作失败的,日志抛出的错误的 Commands out of sync; you can't run this command now。但是把具体的这条sql语句放到可视化工具里面执行却并没有什么问题。所以sql语句并没有什么语法的问题,mysql官方对这个错误的解释是:

 

If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.

This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have calledmysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() ormysql_store_result() in between.

大概的意思就是说你在使用 mysql_use_result() 之前并没有调用mysql_free_result(),或者是你尝试执行两次查询,但是在两次查询的中间你并没有对返回数据调用mysql_use_result() ormysql_store_result()就行处理。

排查了所有的带结果集的sql操作,也都进行了mysql_free_result。多次定位发现,程序是调用了一个多条update语句拼接的一个sql命令。 这条update语句是执行成功了,但是却导致了后续的所有mysql错误都是抛出Commands out of sync; you can't run this command now 的错误。

在mysql在connect函数中可以指定CLIENT_MULTI_STATEMENTS 选项来一次执行通过分号分割的多条sql语句。通过查阅资料发现对于一次执行多条update(或者insert)这类无结果集的sql操作我们也需要对其进行ysql_free_result,在sql语句执行完后加上下面的代码便解决该问题

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

有点奇怪,为什么result明明获取出来是NULL,但还是要执行mysql_free_result操作。希望有明白之人可以讲解一下

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值