mysql-python:commands out of sync
MAR 13TH, 2012
This error has been encountered when adding new functionality to the MySQL database access layer:
|
Programmingerror: (2014, "Commands out of sync;" Can ' t run this command now ' |
Have seen a few times before, because the frequency is very low, do not care too much, this time for a reason, the MySQL document on the Commands out of the sync error description is this:
If you have Commands out of sync; You can ' t run this command now in your client code, and you are calling client functions to the wrong order.
This can happen, for example, if your are using Mysql_use_result () and try to execute a new query before your have called my Sql_free_result (). It can also happen if you try to execute two queries this return data without calling Mysql_use_result () or mysql_store_re Sult () in between. Commands out of sync dev.mysql.com/doc/refman/5.1/en/...
MySQL and client communications using a "Half-duplex" response protocol, the client each send a query, the server "forcibly push" results to the client, the client needs to perform mysql_use_result () or Mysql_store_result () from the server side to retrieve the results, This is a complete query interaction, and submitting the query without retrieving the result will result in Commands out of sync.
Because of the use of Mysql-python, it is not likely that the first condition to call Mysql_free_result () is not possible, and MYSQLDB's default cursor uses Mysql_store_result () instead Mysql_use_result (), so should be the second case.
Grabbed some queries that might be causing the problem, and found statements like this:
|
SELECT A, b from T LIMIT 1; --Some comments |
After you have executed such a query, you will encounter Commands out of sync problems with any further queries.
Because this is actually a semicolon-separated two separate query statements, used MYSQLDB execution, as a statement sent to the MySQL server, after MySQLdb executed a mysql_store_result ().
Before you need to automatically add comments to each query, and the individual query has a semicolon at the end, and then the problem is triggered after appending the comment, just strip the semicolon before adding the comment.
MYSQLDB has four kinds of cursor:
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."""
The default is cursor, which executes mysql_store_result () after the query is submitted, and retrieves all the data returned by the MySQL Server and caches it locally,sscursor using the mysql_use_result (), the result "cached" On the server side, the client-by-line retrieval of results, the advantage is faster than mysql_store_result () Fast, and the client can save memory, but in a high concurrency environment, Or use the default