3

I am working with postgres 9.3 and Python 2.7.

In a plpython function, I want to execute a query that returns a boolean. How can I get the boolean result?

For example:

result = plpy.execute('select 1<2 ')
0

2 Answers 2

5

I figured out how to do this:

query="select 1<2 as val;"
result=plpy.execute(query)
if result[0]["val"]: 
   print 'of corse: 1 < 2'
else:
   print 'this will never be printed'
Sign up to request clarification or add additional context in comments.

Comments

1

In your example result should be a list of rows. It's in the documentation here

3 Comments

yes, the result is a list of rows, we get an element of row by typing result[i]["my_column"]. But when the query returns a boolean, how can we know it is true or false ?
Maybe I'm missing something else, but you use any python construct you want to test the truthiness of it, e.g. "x = result[i]["my_column"]; if x is True: pass". You're more likely to get better help if you post what you've already done.
Thank you yieldsfalsehood, I have just figured out how to do that, please check my answer.

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.