I need to update some rows on the MySQL database by PyMYSQL and I want to know how many rows had been changed.
import pymysql
db = pymysql.connect(xxxx)
cur = db.cursor()
sql = "update TABLE set A = 'abc' where B = 'def'"
cur.execute(sql, params)
db.commit()
You can do this by cursor.rowcount
after executing stage. Ff return value of this is opposite of 0 it means one or more rows are affected.
Mysql provides a special call that will help you achieve exactly that: mysql-affected-rows
. 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 here.
How to use this in Python? The return of execute
command will return you exactly this.
affected_rows = cur.execute(sql, params)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With