MySQLdb: execute query results...

Discussion in 'Python' started by Sheila King, Sep 1, 2003.

  1. Sheila King

    Sheila King Guest

    I have searched for an answer to this question on the comp.lang.python
    archives at Google Groups, and also looked through the (sparse) MySQLdb
    documentation, and cannot confirm for a FACT what I think I know.

    Looking for affirmation before I go "assuming" something and getting myself
    into trouble somewhere along the line...

    Here is what I *think* I know...let me know if I'm wrong, for crying out
    loud!!!

    If I do something like:

    >>> db = MySQLdb.connect(passwd="mypass", host="localhost",

    .... db="mydb", user="myusername")
    >>> c = db.cursor()


    and then later do:

    >>> result = c.execute("""<some proper MySQL query here>""")


    doesn't result always return the number of affected rows?

    As a more concrete example:

    I made the following table:

    mysql> CREATE TABLE example(
    -> name VARCHAR(14),
    -> AGE INT,
    -> COUNTRY VARCHAR(15));
    Query OK, 0 rows affected (0.03 sec)


    And then in Python did the following INSERTS and UPDATE:

    >>> c.execute("""INSERT INTO example (name, AGE, COUNTRY) VALUES ("sheila", 29, "US");""")

    1L
    >>> c.execute("""INSERT INTO example (name, AGE, COUNTRY) VALUES ("arthur", 23, "NL");""")

    1L
    >>> c.execute("""INSERT INTO example (name, AGE, COUNTRY) VALUES ("bob", 29, "US");""")

    1L
    >>> result = c.execute("""UPDATE example SET AGE=30 WHERE AGE=29;""")
    >>> result

    2L


    Can I *RELY* on that "2L" result of the UPDATE statement under the cursor's
    "execute" method representing the number of rows updated? Or am I assuming
    too much?

    Like I say, I looked through several sources, including my O'Reilly MySQL
    book and cannot verify this for a fact from docs.

    I did find this, however:
    http://www.mysql.com/doc/en/UPDATE.html

    "UPDATE returns the number of rows that were actually changed. In MySQL
    Version 3.22 or later, the C API function mysql_info() returns the number
    of rows that were matched and updated and the number of warnings that
    occurred during the UPDATE. "

    Which seems to corroborate, although they are not referring to the Python
    DB-API specifically.

    Thanks for any clarification on this matter,


    --
    Sheila King
    http://www.thinkspot.net/sheila/
    http://www.k12groups.org/
    Sheila King, Sep 1, 2003
    #1
    1. Advertising

  2. Sheila King

    Sheila King Guest

    On Sun, 31 Aug 2003 19:14:00 -0500, mackstann <> wrote in
    comp.lang.python in article
    <>:

    > Looked in site-packages/MySQLdb/cursors.py, and it seems you are
    > correct.
    >
    > >>> from MySQLdb import cursors
    > >>> help(cursors.BaseCursor.execute)

    >
    > Help on method execute in module MySQLdb.cursors:
    >
    > execute(self, query, args=None) unbound MySQLdb.cursors.BaseCursor
    > method
    > Execute a query.
    >
    > [...]
    >
    > Returns long integer rows affected, if any
    >
    > HTH,


    Thanks for the confirmation. I didn't look at the source code. :/

    Much appreciated,


    --
    Sheila King
    http://www.thinkspot.net/sheila/
    http://www.k12groups.org/
    Sheila King, Sep 1, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Beryl Small
    Replies:
    1
    Views:
    656
    bruce barker
    Dec 2, 2003
  2. Tim Williams
    Replies:
    2
    Views:
    633
    Tim Williams
    May 28, 2004
  3. Carles Pina i Estany

    mysqldb execute timeout

    Carles Pina i Estany, Apr 20, 2007, in forum: Python
    Replies:
    0
    Views:
    603
    Carles Pina i Estany
    Apr 20, 2007
  4. John Nagle
    Replies:
    20
    Views:
    652
    M.-A. Lemburg
    Feb 7, 2008
  5. Eric Nelson
    Replies:
    5
    Views:
    1,511
    Alexey Smirnov
    Feb 4, 2009
Loading...

Share This Page