"Skip Montanaro" <> wrote in message
news:mailman.1059075503.7037.python-...
>
> 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/