我正在使用PostgreSQL9.2编写一个plpython函数。假设代码已经执行了一个返回hstore字符串的查询。然后,我希望发出一个查询:
SELECT hstore_to_matrix('hstorestring')假设它是一个包含hstore字符串的字符串:A=>B
create or replace function testfreq()
returns text
as $$
hstorestring = '"GT"=>"thing","HS"=>"[-0.1,-0.2]"'
rv2 = plpy.execute("SELECT hstore_to_matrix(%s)" % (plpy.quote_literal(hstorestring)))
plpy.log("Hstore:",rv2[0])
return("done")
$$ LANGUAGE plpythonu;运行方式
select testfreq();返回
testdb=# select testfreq();
ERROR: plpy.Error: unrecognized error in PLy_spi_execute_fetch_result
CONTEXT: Traceback (most recent call last):
PL/Python function "testfreq", line 3, in <module>
rv2 = plpy.execute("SELECT hstore_to_matrix(%s)" % (plpy.quote_literal(hstorestring)))
PL/Python function "testfreq": 如果在上面的代码中替换为hstore_to_array,则输出为:
testdb=# select testfreq();
LOG: ('Hstore:', {'hstore_to_array': ['GT', 'thing', 'HS', '[-0.1,-0.2]']})
CONTEXT: PL/Python function "testfreq"
testfreq
----------
done
(1 row)我还尝试使用hstore操作符而不是函数,并且我在pgsql终端中尝试了这些函数,以确保它们不嵌入到python中时也能正常工作。如果有任何建议,我们将非常感谢。
发布于 2013-04-18 08:36:27
看起来PL/Python不能正确处理多维数组:
create or replace function testarray()
returns text
as $$
rv2 = plpy.execute("SELECT ARRAY[ ARRAY['1','2','3'], ARRAY['a','b','c'] ];" )
$$ LANGUAGE plpythonu;结果:
craig=# select testarray();
ERROR: plpy.Error: unrecognized error in PLy_spi_execute_fetch_result
CONTEXT: Traceback (most recent call last):
PL/Python function "testarray", line 2, in <module>
rv2 = plpy.execute("SELECT ARRAY[ ARRAY['1','2','3'], ARRAY['a','b','c'] ];" )
PL/Python function "testarray"(在Pg 9.2.4,Python 2.7.3上测试)。
hstore文本有效:
craig=# SELECT '"GT"=>"thing","HS"=>"[-0.1,-0.2]"'::hstore;
hstore
------------------------------------
"GT"=>"thing", "HS"=>"[-0.1,-0.2]"
(1 row)并且查询在PL/Python之外工作:
craig=# select hstore_to_matrix('"GT"=>"thing","HS"=>"[-0.1,-0.2]"');
hstore_to_matrix
---------------------------------
{{GT,thing},{HS,"[-0.1,-0.2]"}}
(1 row)进一步表明这是一个PL/Python问题。
您可能可以通过转换为text,然后在返回结果后转换回text[]来解决此问题,尽管这样效率很低:
create or replace function testfreq()
returns text
as $$
hstorestring = '"GT"=>"thing","HS"=>"[-0.1,-0.2]"'
rv2 = plpy.execute("SELECT hstore_to_matrix(%s)::text" % (plpy.quote_literal(hstorestring)))
plpy.log("Hstore:",rv2[0])
return("done")
$$ LANGUAGE plpythonu; 结果:
craig=# SELECT testfreq();
testfreq
----------
done
(1 row)https://stackoverflow.com/questions/16049313
复制相似问题