cx_Oracle, is anything selected?

D

Damjan

Is there a way to see if the SELECT in cx_Oracle didn't return anything?
I want to optimize the situation when the number of selected rows is zero.
Is select count(*) the only option, seems inefficient?
 
D

Diez B. Roggisch

Damjan said:
Is there a way to see if the SELECT in cx_Oracle didn't return anything?
I want to optimize the situation when the number of selected rows is zero.
Is select count(*) the only option, seems inefficient?

I don't understand your problem - if your select doesn't return
anything, the fetch* methods on the cursor will tell you if there is any
data to expect at all. Additionally there is teh rowcount-property that
holds the number of rows the last execute* yielded.

Diez
 
D

Damjan

Is there a way to see if the SELECT in cx_Oracle didn't return anything?
I don't understand your problem - if your select doesn't return
anything, the fetch* methods on the cursor will tell you if there is any
data to expect at all. Additionally there is teh rowcount-property that
holds the number of rows the last execute* yielded.

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.

c.rowcount will not give me the number of rows selected, it will only give
me the number of rows already fetched.
 
D

Diez B. Roggisch

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.

c.rowcount will not give me the number of rows selected, it will only give
me the number of rows already fetched.

then lazy-initialize the smpt object inside the loop. Or use c.fetchone
for the first entry, and loop over the remaining results.

Apart from that: what harm does the connection to the smpt do? If it
works - keep it that way.

Regards,

Diez
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

Damjan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,028
Latest member
chasetony

Latest Threads

Top