Re: [newbie] MySQL : How to check if no row returned?

Discussion in 'Python' started by Skip Montanaro, Jul 24, 2003.

  1. Jane> I browsed through the archives of this site, but I didn't see how
    Jane> to check if a SELECT through the MySql module returns an empty set
    Jane> (whether a row exists or not, the "else" part below is always
    Jane> called).

    The return value of the cursor's execute method indicates how many rows were
    selected:

    >>> import MySQLdb
    >>> conn = MySQLdb.Connection(...)
    >>> curs = conn.cursor()
    >>> print curs.execute("select * from cities where city like 'San %'")

    51
    >>> rows = curs.fetchall()
    >>> print len(rows)

    51

    Skip
    Skip Montanaro, Jul 24, 2003
    #1
    1. Advertising

  2. Skip Montanaro

    Steve Holden Guest

    "Skip Montanaro" <> wrote in message
    news:...
    >
    > Jane> I browsed through the archives of this site, but I didn't see

    how
    > Jane> to check if a SELECT through the MySql module returns an empty

    set
    > Jane> (whether a row exists or not, the "else" part below is always
    > Jane> called).
    >
    > The return value of the cursor's execute method indicates how many rows

    were
    > selected:
    >
    > >>> import MySQLdb
    > >>> conn = MySQLdb.Connection(...)
    > >>> curs = conn.cursor()
    > >>> print curs.execute("select * from cities where city like 'San %'")

    > 51
    > >>> rows = curs.fetchall()
    > >>> print len(rows)

    > 51
    >


    Alternatively, you can observer that the fetchall() method will return an
    empty list, and use

    if not rows:
    # empty set returned

    This is a bit more portable, which is maybe not important, but if you
    planned to distribute your software to users with other database platforms,
    not all DBI modules' execute() methods return the row count.

    regards
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/pwp/
    Steve Holden, Jul 24, 2003
    #2
    1. Advertising

  3. Skip Montanaro

    Jane Doe Guest

    On Thu, 24 Jul 2003 21:05:13 GMT, "Steve Holden"
    <> wrote:
    >Alternatively, you can observer that the fetchall() method will return an
    >empty list, and use
    >
    > if not rows:
    > # empty set returned


    Thx to you both. That did it :)

    JD.
    Jane Doe, Jul 25, 2003
    #3
    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. JL
    Replies:
    0
    Views:
    1,142
  2. =?Utf-8?B?SklNLkgu?=

    number of row returned

    =?Utf-8?B?SklNLkgu?=, Jul 24, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    3,547
  3. shikari shambu
    Replies:
    0
    Views:
    238
    shikari shambu
    Dec 16, 2006
  4. D
    Replies:
    0
    Views:
    203
  5. CJM
    Replies:
    11
    Views:
    258
    Alexander Kuznetsov
    Jul 12, 2006
Loading...

Share This Page