MySQLdb Python Update Matched row versus Changed rows
mysql, mysql-python, python
Solution
By default, `db.affected_rows()` and `cursor.rowcount` should give you the number of changed rows. The FOUND_ROWS option makes `cursor.rowcount` return the number of matched rows instead:
db_connection = MySQLdb.connect(
host = settings['dbHost'],
user = settings['dbUser'],
passwd = settings['dbPass'],
db = settings['dbName'],
client_flag = MySQLdb.constants.CLIENT.FOUND_ROWS
)
For two ways to get both the number of matched rows and the number of changed rows, check out this question:
How to get matched Rows from MySQLdb.cursors.Cursor python2.6
Problem
From the mysql client command line, I get useful information after an UPDATE telling me both how many rows were matched by the WHERE clause, and how many were actually changed. In the case where I'm setting the values to existing values, I get 1 matched and 0 changed. This is very useful information I would like to capture in my Python MySQLdb script. I have tried db.affected_rows() and cursor.rowcount but they always come out to be 1 (seems to be the "matched" value). I've tried with and without commit. How can I find both the Match value and the Changed value in Python? ``` mysql> UPDATE userlogtable SET somefield="samedata" WHERE idxfield=1; Query OK, 0 rows affected (0.02 sec) Rows matched: 1 Changed: 0 Warnings: 0 mysql> UPDATE userlogtable SET somefield="newdata" WHERE idxfield=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 ``` Related but not duplicate question(s): Python MYSQLdb documentation missing details?