python - how to check if value return in plpython query? -
i have assignment in plpythonu
in postgresql function:
result=plpy.execute("select value table doctitle '%%%s%%'"%projectname) if result: final = result[0]["value"]
the query can return 1 text result or nothing.
the issue if query returned nothing if result:
condition still true
(cuz contains value column)... want assignment final
happen if there actual value in value column. how check that?
the result object emulates list or dictionary object. check length:
if len(result) > 0:
but test should work empty list or dictionary evaluates false
:
create or replace function p() returns boolean $$ result = plpy.execute('select 1 false') if result: return true else: return false $$ language plpythonu;
returns false
:
select p(); p --- f
pass parameters execute
in instead of substitute yourself. otherwise vulnerable sql injection:
query = """ select value table doctitle format('%%%s%%', $1) """ plan = plpy.prepare(query, ["text"]) result = plpy.execute(plan, [_text_parameter]) if result: final = result[0]["value"]
https://www.postgresql.org/docs/current/static/plpython-database.html
Comments
Post a Comment