错误出现在调用了存储过程之后继续调用了返回结果集的命令。

这个错误的官方解释为:

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 called mysql_free_result().

It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.

地址为:Commands out of sync

执行了一个存储过程, 需要将结果集取完,然后才能调用下一个返回结果集的命令。


示例如下(Connector/C++中的处理办法):

存储过程:

BEGINselect * from gamebase where Accountid = AccID;
END

代码片段1:

newConnection.SetStatement("CALL LoadGameBase(1)");
newConnection.Execute();auto resultSet = newConnection.GetResultSet();
while (resultSet.Next())
{int accountID = resultSet.GetInt(1);std::string accountName = resultSet.GetString(2);double curGold = resultSet.GetDouble(3);std::cout << accountID << " " << accountName << " " << curGold << std::endl;
}

上面这种执行方式就会报错为"Commands out of sync…"

正确的方式为:
代码片段2:

newConnection.SetStatement("CALL LoadGameBase(1)");
newConnection.Execute();do
{auto resultSet = newConnection.GetResultSet();while (resultSet.Next()){int accountID = resultSet.GetInt(1);std::string accountName = resultSet.GetString(2);double curGold = resultSet.GetDouble(3);std::cout << accountID << " " << accountName << " " << curGold << std::endl;}
}
while (newConnection.GetMoreResults());

使用成员函数GetMoreResults遍历结果集。
注意上面GetMoreResults的调用方式,
只能使用do、while,这是因为调用GetMoreResults之前,"结果集指针"就已经指向了第一个结果集,
调用之后,将会指向下一个。

下面是错误示例:
代码片段3:

newConnection.SetStatement("CALL LoadGameBase(1)");
newConnection.Execute();while (newConnection.GetMoreResults())
{auto resultSet = newConnection.GetResultSet();while (resultSet.Next()){int accountID = resultSet.GetInt(1);std::string accountName = resultSet.GetString(2);double curGold = resultSet.GetDouble(3);std::cout << accountID << " " << accountName << " " << curGold << std::endl;}
}

执行后不会报错,但也不会得到任何数据。

还有一点需要注意:
如果执行完存储过程后,完全不取结果集,是可以直接执行下一个返回结果集的语句。
可以理解为:存储过程返回的结果集,要么不取,要么取完。

MySQL error(2014) Commands out of sync; you can't run this command now(情形1)相关推荐

  1. 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语句放到可视化 ...

  2. commands out of sync mysql_MySQL问题一则:Commands out of sync; you can't run this command now以及相关问题...

    录制程序有一功能:将录制的文件信息写入MySQL数据库,供BS系统查询. 因此封装了一个MySQL类,进行数据库操作. 主要接口为Update():执行SQL语句. 现在问题来了: (一)在某个场景下 ...

  3. mysql执行存储过程提示out of_PHP执行MYSQL存储过程报错:Commands out of sync; you can't run...

    php中在同时执行2个存储过程时,有一个程序2个储存过程都执行,有一个程序只执行第一个调用.2个都执行的调用如下: $mydb->query("delete from pinfo wh ...

  4. commands out of sync mysql,MySQL-python: Commands out of sync

    在给 MySQL 数据库访问层增加新功能时遇到了这样的错误: ProgrammingError: (2014, "Commands out of sync; you can't run th ...

  5. Mysql++关于多数据集查询Commands out of sync; you can‘t run this command now的问题

    今天在项目中使用了Mysql++调用Mysql的存储过程来实现功能,但是碰到一个有意思的问题,当成功调用存储过程后,再次做其他的查询,确返回Commands out of sync; you can' ...

  6. commands out of sync错误

    第一种情况: commands out of sync.  Did you run multiple statements at once 可能的原因:是在事务之中写了查询语句. 解决方案:把查询语句 ...

  7. 「golang」panic: commands out of sync. Did you run multiple statements at once

    [问题现象] go使用go-sql-driver连接mariaDB,如下,提示: panic: commands out of sync. Did you run multiple statement ...

  8. db mysql error list / mysql tools / mysql error_list

    db mysql http://lindows.iteye.com/admin/blogs/232318 http://dev.mysql.com/doc/refman/5.1/zh/error-ha ...

  9. Linux下mysql ERROR 1045,Mysql无法执行批处理

    Linux下mysql ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO) 每次启用mysql都出现这个 ...

  10. mysql error.log 权限_docker-compose 启动mysql 容器时 error.log 无权限访问怎么办?

    场景分析 docker-compose 运行mysql 容器的时候一直报下面的错误: .../var/log/mysql/mysql-error.log' for error logging: Per ...

最新文章

  1. 图片出处识别_图片模糊怎么变清晰?方法都在这里了
  2. 看中了一辆捷安特自行车 CFR5100
  3. java怎么画词云_Matplotlib学习---用wordcloud画词云(Word Cloud)
  4. Velocity引擎导致jvm内存外内存泄露
  5. ajax 上传读取excel
  6. MySQL数据库 基本操作语句
  7. python修改文件内容,不需要read,write多个动作。
  8. android第一天-------环境搭建
  9. 代码实现从键盘接收一个字符串, 程序对其中所有字符进行排序,例如键盘输入: helloitcast程序打印:acehillostt...
  10. eclipse配置tomcat运行时访问路径不要项目名称
  11. Oracle DBHelper
  12. 获取一个类的信息(仿YYClassInfo类)
  13. 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程
  14. 逻辑回归(LR) 算法模型简介
  15. 老师常用选择题,选择框,单选框,以及各行业产品配置表单选配置明细等
  16. 知识图谱研究最新综述论文: 表示学习、知识获取与应用
  17. Ubuntu16.04.4制作自定义安装光盘方案
  18. 深度学习之车牌识别(中国、越南)
  19. 贷款申请最大化利润-机器学习项目实战
  20. Wireshark抓包——TCP协议分析

热门文章

  1. 程序员玩连连看的正确姿势
  2. Eclipse 快捷键的使用
  3. react-router 4v 路由嵌套问题
  4. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
  5. MOOON-agent系统设计与使用说明
  6. GPU程序在GameByro中的使用
  7. 10.Kong入门与实战 基于Nginx和OpenResty的云原生微服务网关 --- 内置插件
  8. 1.Docket 安装简介
  9. 28.程序管理(ps,top)
  10. 7. JavaScript RegExp 对象