开发者

how to parameter-ize a query's WHERE clause within pl/python?

Friends:

Have been trying to parameter-ize into a query in pl/python - and am surely missing something simple here; I've tried % and $ prepends to the variable name, with no luck.

(also haven't been able to pass a result variable into the python log - but this is a diff problem! I do have the log set up - can sent literal strings to it - but have cut out lots of code for clarity here)

CREATE OR REPLACE FUNCTION footest0 (param_key integer) RETURNS text AS $$

# 1) SELECT from record based on parameter param_key:

rv = plpy.execute("SELECT name_key,address_key FROM file WHERE foreign_key = para开发者_Python百科m_key)

name = rv[0]["name"]
address = rv[0]["address"]

# how to put results into the log?:
logging.info('  record: %(case)s')

$$ LANGUAGE plpythonu;


Use the $n notation:

st = "SELECT name_key,address_key FROM file WHERE foreign_key = $1"
pst = plpy.prepare(st, [ "integer" ])
rv = plpy.execute(pst, [ param_key ])


I'm not sure exactly what you're asking but if you're trying to parametrize the columns you're using to control a join on a query, parameters don't work that way.

If you're really trying to do that, you can always use string formatting to create your SQL query, like the following:

rv = plpy.execute("SELECT name_key, address_key FROM file WHERE %s = %s" %
                  (foreign_key, param_key))

If you're simply looking to compare foreign_key to a string, Paulo has the answer you're looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜