Python Commands out of sync; Can ' t run this command now

Source: Internet
Author: User
Tags mongodb postgresql redis

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  


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.