python Commands out of sync; you can't run this command now
2016-12-18 22:36
806 查看
MySQL-python: Commands out of sync
MAR 13TH, 2012在给 MySQL 数据库访问层增加新功能时遇到了这样的错误:
1 | ProgrammingError: (2014, "Commands out of sync; you can't run this command now") |
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.
Commands out of sync dev.mysql.com/doc/refman/5.1/en/…
MySQL 和客户端通信使用的是“半双工”的应答协议,客户端每发送一个查询,服务器“强行推送”结果给客户端,客户端需要执行
mysql_use_result()或
mysql_store_result()从服务器端取回结果,这是一次完整的查询交互,只提交查询而不取回结果就会导致
Commands out of sync。
由于使用的是 MySQL-python,所以第一种情况所说的没有调用
mysql_free_result()的问题不大可能存在,并且
MySQLdb 的默认 cursor 使用的是
mysql_store_result()而不是
mysql_use_result(),所以应该是第二种情况。
抓取了一些有可能会导致这个问题的查询,发现了类似这样的语句:
1 | SELECT a, b FROM t LIMIT 1; -- some comments |
Commands out of sync问题。
因为这其实是由分号隔开的两个独立的查询语句,使用 MySQLdb 执行时,作为一个语句发送给 MySQL server,之后 MySQLdb 执行了一次
mysql_store_result()。
之前需要自动地给每一个查询增加注释,而个别的查询在末尾写了分号,追加注释之后就触发了这个问题,那么只需要在加注释前 strip 掉分号就好了。
MySQLdb 有四种 cursor:
12 | class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn, BaseCursor): """This is the standard Cursor class that returns rows as tuples and stores the result set in the client.""" class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn, BaseCursor): """This is a Cursor class that returns rows as dictionaries and stores the result set in the client.""" class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn, BaseCursor): """This is a Cursor class that returns rows as tuples and stores the result set in the server.""" class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn, BaseCursor): """This is a Cursor class that returns rows as dictionaries and stores the result set in the server.""" |
Cursor,在提交查询之后执行
mysql_store_result(),将
MySQL Server 返回的所有数据都取回并缓存在本地,
SSCursor则是使用
mysql_use_result(),将结果“缓存”在服务器端,客户端逐行取回结果,好处是速度会比
mysql_store_result()快,并且客户端可以节省内存,但在高并发环境下,还是使用默认的
Cursor比较好,因为
MySQL server 只有在客户端取回所有的结果之后才会释放相关的锁(如果有的话),而逐行取回并分别对每行做处理大多会需要更长的时间。
本来想所有语句一次性执行,用了;来连接sql语句,于是便出现了该错误,最后解决方案是每个sql分别execute
原文链接
相关文章推荐
- python mysql 2014 Commands out of sync; you can't run this command now
- ERROR (ProgrammingError) (2014, "Commands out of sync; you can't run this command now")
- MySQL一次执行多条语句的实现及常见问题(C API Commands out of sync; you can't run this command now)
- MySQL一次执行多条语句的实现及常见问题(C API Commands out of sync; you can't run this command now)
- Commands out of sync; you can't run this command now SQL
- 解决error:2014 Commands out of sync; you can't run this command now
- PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now
- PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
- PHP+MySQL连续插入数据报错:Commands out of sync; you can't run this command now & 读取Excel数据并批量插入到数据中
- Mysql::Error: Commands out of sync; you can't run this command now: SHOW TABLES解决方案
- Commands out of sync; you can't run this command now
- 解决mysql error:2014 Commands out of sync; you can't run this command now
- C++ Mysql调用 错误:2014 :Commands out of sync; you can't run this command now
- 解决error:2014 Commands out of sync; you can't run this command now
- 解决error:2014 Commands out of sync; you can't run this command now
- Commands out of sync; you can't run this command now
- LR错误: Commands out of sync; you can't run this command now 解决方法
- php用mysql_query()调用存储过程后无法再执行sql报错:commands out of sync you can't run this command now
- Commands out of sync; you can't run this command now
- PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now