Daniel Wilson
asked on
PL/Python NameError: global name '<variable>' is not defined
I'm trying to create a PL/Python function for PostgreSQL. Here's a simplified version that gets the error.
When I call the function with the SELECT statement, it complains
If I hardcode a value in, rather than using my variable, it works. What am I doing wrong?
Thanks!
create or replace function eztest( ASIN varchar(10)) returns integer AS
$$
plan = plpy.prepare("Select bookid from book where ASIN = $1", ["text"])
rv = plpy.execute(plan, [ ASIN ])
return rv["bookid"]
$$ language plpython2u;
Select eztest('B00DMYO2D2');
When I call the function with the SELECT statement, it complains
ERROR: NameError: global name 'ASIN' is not defined
CONTEXT: Traceback (most recent call last):
PL/Python function "eztest", line 3, in <module>
rv = plpy.execute(plan, [ ASIN ])
PL/Python function "eztest"
If I hardcode a value in, rather than using my variable, it works. What am I doing wrong?
Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Cool. Is this requirement specified somewhere?
ASKER
I haven't found a spec that says this. So my results should be considered anecdotal and applying to my particular Ubuntu 12.04 LTS installation running PostgreSQL 9.3 and running under VMWare Player. But ... perhaps ... the same solution will work in other contexts.
ASKER
As for mapping Postgres data types to Python, that is specified:
http://www.postgresql.org/docs/9.3/static/plpython-data.html#AEN60813
Any of the character types maps to a Python str.
http://www.postgresql.org/docs/9.3/static/plpython-data.html#AEN60813
Any of the character types maps to a Python str.
ASKER
Thanks for the ideas!