How to get rows affected in a UPDATE statement by PyMSQL
<p>See the title,I need to update some rows on mysql database by PyMYSQL and I want to know <strong>how many rows had been changed.</strong></p>

<p>code:
<code>
    import pymysql
    db = pymysql.connect(xxxx)
    cur = db.cursor()
    sql = "update TABLE set A = 'abc' where B = 'def'"
    cur.execute(sql, params)
    db.commit()
</code></p>

<p>Mysql provides a special call that will help you achieve exactly that: <code>mysql-affected-rows</code>. This function is especially useful on updates as it will return only the number of rows that were affected, not the ones where the updated value was similar. Documentation is <a href="https://dev.mysql.com/doc/refman/5.6/en/mysql-affected-rows.html" rel="nofollow">here</a>.</p>

<p>How to use this in Python? The <a href="http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb.cursors.BaseCursor-class.html#execute" rel="nofollow">return</a> of <code>execute</code> command will return you exactly this.</p>

<pre><code>affected_rows = cur.execute(sql, params)
</code></pre>

python mysql pymysql