Python: Number of rows affected by cursor.execute("SELECT …)

前端 未结 6 1378
迷失自我
迷失自我 2020-12-13 08:36

How can I access the number of rows affected by:

cursor.execute(\"SELECT COUNT(*) from result where server_state=\'2\' AND name LIKE \'\"+digest+\"_\"+charse         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 08:54

    The number of rows effected is returned from execute:

    rows_affected=cursor.execute("SELECT ... ")
    

    of course, as AndiDog already mentioned, you can get the row count by accessing the rowcount property of the cursor at any time to get the count for the last execute:

    cursor.execute("SELECT ... ")
    rows_affected=cursor.rowcount
    

    From the inline documentation of python MySQLdb:

     def execute(self, query, args=None):
    
        """Execute a query.
    
        query -- string, query to execute on server
        args -- optional sequence or mapping, parameters to use with query.
    
        Note: If args is a sequence, then %s must be used as the
        parameter placeholder in the query. If a mapping is used,
        %(key)s must be used as the placeholder.
    
        Returns long integer rows affected, if any
    
        """
    

提交回复
热议问题