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

Skip Montanaro skip at pobox.com
Thu Jul 24 21:36:45 CEST 2003


    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





More information about the Python-list mailing list