cx_Oracle, is anything selected?

Gerhard Häring gh at ghaering.de
Tue Nov 1 08:34:52 EST 2005


Damjan wrote:
> This is a simplification of the program
> 
> c = db.cursor()
> while 1:
>     c.execute('select ....')
>     smtp = SMTP(MAIL_HOST, 25, 'localhost')
>     for address, subject, body in c:
>         smtp.sendmail(....)
>     smtp.quit()
>     time.sleep(60)
> 
> now if the select doesn't return any rows, there's no need to connect to the
> mail server. I'd like to avoid that unnecessary step. [...]

Well, something like:

c = db.cursor()
while 1:
     smtp = None
     c.execute('select ....')
     for address, subject, body in c:
         if not smtp:
             smtp = SMTP(MAIL_HOST, 25, 'localhost')
         smtp.sendmail(....)
     if smtp:
         smtp.quit()
     time.sleep(60)


should do that.

-- Gerhard




More information about the Python-list mailing list