Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: [newbie] MySQL : How to check if no row returned?

Reply
Thread Tools

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

 
 
Skip Montanaro
Guest
Posts: n/a
 
      07-24-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

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      07-24-2003
"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/



 
Reply With Quote
 
 
 
 
Jane Doe
Guest
Posts: n/a
 
      07-25-2003
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM
"mysql.h: No such file or directory" when building MySQL-python francescomoi@europe.com Python 2 05-11-2005 03:12 PM
DBD:mysql doesn't read mysql option file /etc/my.cnf file JL Perl 0 01-28-2005 03:19 AM
"Pure Python" MySQL module like Net::MySQL Ravi Python 6 07-21-2003 06:53 PM



Advertisments