MySQLdb -- any way to get "rows matched"?

Skip Montanaro skip at pobox.com
Tue Sep 2 15:04:20 EDT 2003


    mysql> UPDATE example SET AGE=30 WHERE AGE=30;
    Sheila> Query OK, 0 rows affected (0.00 sec)
    Sheila> ws matched: 2  Changed: 0  Warnings: 0


    Sheila> MySQLdb in Python:

    >>>> c = conn.cursor()
    >>>> c.execute("""UPDATE example SET AGE=30 WHERE AGE=30;""")
    Sheila> 0L
    >>>> c.messages
    Sheila> []
    >>>> c.info()
    Sheila> ''

    Sheila> Being a MySQL and MySQLdb newbie, I'm not sure if there is any
    Sheila> way to find out how many matched, via Python's MySQLdb module. I
    Sheila> thought, from the source code, that .messages or .info() for the
    Sheila> cursor attributes/methods might do it, but apparently not???

I'm not sure what the return value means when executing an update.  My guess
is that it returns the number of rows changed.  If you want to know the
number of rows matched, execute a SELECT which matches your WHERE clause:

    >>> db = MySQLdb.Connection(...)
    >>> c = db.cursor()
    >>> c.execute("select * from cities where city='San Jose'")
    4L
    >>> c.execute("update cities set city='San Jose' where city='San Jose'")
    0L

Skip





More information about the Python-list mailing list