2

I need to count the number of rows returned from database.By using following code

rv = plpy.execute("SELECT * FROM AA where name = 'active '") 
rv[0]["id"] 

works when some values are returned but I need to check if no values are returned: Can someone help please.

1
  • 1
    It might have helped to emphasize that this is PL/Python. Commented Apr 16, 2011 at 19:23

4 Answers 4

4

Why don't you just check the size of the rv array?

Sign up to request clarification or add additional context in comments.

2 Comments

I am quite new to postgresql, can you please tell how to check the size of rv array. That will be highly appreciated. Thank you.
Thats not postgres related, it's standard python syntax.
3

Why dont you use -

SELECT Count(*) as rowCount FROM AA where name = 'active'

This will return you the number of rows from the query. If there are no rows, then it will return 0.

1 Comment

because he is looking to get something not just count. Additionally needs to know whether or not there was any result. Using your method would take 2 queries.
3

In PL/Python, the way to get the result size is

rv.nrows

See the documentation.

Comments

-1

.rowcount

1 Comment

This is correct in DB-API, but not in PL/Python, which is what is being used here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.