commands out of sync mysql,MySQL-python: Commands out of sync
在给 MySQL 数据库访问层增加新功能时遇到了这样的错误:
ProgrammingError: (2014, "Commands out of sync; you can't run this command now")
之前零星地见到过几次,因为发生频度很低,就没有太在意,这次找了一下原因,MySQL 文档对 Commands out of sync 错误的描述是这样的:
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.
MySQL 和客户端通信使用的是“半双工”的应答协议,客户端每发送一个查询,服务器“强行推送”结果给客户端,客户端需要执行 mysql_use_result() 或 mysql_store_result() 从服务器端取回结果,这是一次完整的查询交互,只提交查询而不取回结果就会导致 Commands out of sync。
由于使用的是 MySQL-python,所以第一种情况所说的没有调用 mysql_free_result() 的问题不大可能存在,并且 MySQLdb 的默认 cursor 使用的是 mysql_store_result() 而不是 mysql_use_result(),所以应该是第二种情况。
抓取了一些有可能会导致这个问题的查询,发现了类似这样的语句:
SELECT a, b FROM t LIMIT 1; -- some comments
执行了这样的查询之后,再执行任何查询都会遇到 Commands out of sync 问题。
因为这其实是由分号隔开的两个独立的查询语句,使用 MySQLdb 执行时,作为一个语句发送给 MySQL server,之后 MySQLdb 执行了一次 mysql_store_result()。
之前需要自动地给每一个查询增加注释,而个别的查询在末尾写了分号,追加注释之后就触发了这个问题,那么只需要在加注释前 strip 掉分号就好了。
MySQLdb 有四种 cursor:
class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn,BaseCursor):"""This is the standard Cursor class that returns rows as tuplesand stores the result set in the client."""class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn,BaseCursor):"""This is a Cursor class that returns rows as dictionaries andstores the result set in the client."""class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn,BaseCursor):"""This is a Cursor class that returns rows as tuples and storesthe result set in the server."""class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn,BaseCursor):"""This is a Cursor class that returns rows as dictionaries andstores the result set in the server."""
默认使用的是 Cursor,在提交查询之后执行 mysql_store_result(),将 MySQL Server 返回的所有数据都取回并缓存在本地,SSCursor 则是使用 mysql_use_result(),将结果“缓存”在服务器端,客户端逐行取回结果,好处是速度会比 mysql_store_result() 快,并且客户端可以节省内存,但在高并发环境下,还是使用默认的 Cursor 比较好,因为 MySQL server 只有在客户端取回所有的结果之后才会释放相关的锁(如果有的话),而逐行取回并分别对每行做处理大多会需要更长的时间。
Reference:
commands out of sync mysql,MySQL-python: Commands out of sync相关推荐
- mysql connector python linux_MySQL Connector/Python 安装、测试
安装Connector/Python: # wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-1. ...
- 【原创】MySQL 以及 Python 实现排名窗口函数
大部分数据库都提供了窗口函数,比如RANK,ROW_NUMBER等等. MySQL 这方面没有直接提供,但是可以变相的实现,我以前写了row_number 的实现,今天有时间把 rank 的实现贴出来 ...
- python django mysql安装_Django+Nginx+uWSGI+Mysql搭建Python Web服务器
原标题:Django+Nginx+uWSGI+Mysql搭建Python Web服务器 安装的时候全部选择英文,记得以前选择中文的时候安装时出了问题,服务器组件一个不选,Ubuntu安装做的很贴心,基 ...
- excel了解mysql_对比Excel、MySQL、Python,分别讲述 “if函数” 的使用原理!
其实,不管是Excel.MySQL,还是Python,"if"条件判断都起着很重要的作用.今天这篇文章,就带着大家盘点一下,这三种语言如何分别使用"if函数" ...
- mysql connector python linux_Python使用mysql.connector链接mysql数据库
之前一直使用pythond mysqldb链接数据库,随着mysql被oracle收购之后,我发现mysqldb就不怎么更新了. 现在开始使用oracle提供的mysql.connector来操作my ...
- Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器
Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器 闲着无聊的时候部署了一个Django项目玩,用vm虚拟机部署的. 准备工作 我使用的系统是Ubuntu16 ...
- MySQL Connector / Python
MySQL Connector / Python允许Python程序使用符合Python数据库API规范v2.0(PEP 249)的API访问MySQL数据库 . MySQL Connector / ...
- Eclipse+PyDev+Django+Mysql搭建Python web开发环境
Eclipse+PyDev+Django+Mysql搭建Python web开发环境 Python的web框架有很多,目前主流的有Django.Tornado.Web.py等,最流行的要属Django ...
- django mysql connector,MySQL Connector / python在Django中不起作用
我正在学习以MySQL为后端的Django. 我安装了Oracle的mysql连接器以与mysql连接. 但是,当我运行python manage.py时,出现此错误 Traceback (most ...
- mysql 嵌套if标签_对比Excel、MySQL、Python,分别讲述 “if函数” 的使用原理!
作者:黄伟呢 本文转自:数据分析与统计学之美 其实,不管是Excel.MySQL,还是Python,"if"条件判断都起着很重要的作用.今天这篇文章,就带着大家盘点一下,这三种语言 ...
最新文章
- 深度学习必备:随机梯度下降(SGD)优化算法及可视化
- 动态调试android, ddmsida
- 【PAT乙级】1005 继续(3n+1)猜想 (25 分)
- bind、delegate、on的区别
- 疯子的算法总结(二) STL Ⅰ 算法 ( algorithm )
- 彻底解决zend studio 下 assignment in condition警告
- 129_Power PivotPower BI DAX不同维度动态展示动态坐标轴
- 利用继承和组合实现软件复用
- 输出一串小于100的随机数(rand())
- bzoj 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐(DP)
- 隐式反馈的去噪,模型取得巨大提升!
- 90.精读《极客公园 2019》
- Android常用框架收藏
- 欢迎你、某某某同学python_python123第一周作业
- Unity URP中的Single-Pass到底是什么
- 软件“生命”系统进化论——软件以负熵为生!
- maven打包报错Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.5.2.RELEASE:repa
- 2022网络工程师下午大纲(附带华为交换机开局配置一本通)
- html5兼容问题,【HTML5分享】关于浏览器的兼容问题
- 基于单片机的CO浓度检测及报警系统设计(电路+程序流程)