gpt4 book ai didi

python - 在 Python 中创建 Postgres 函数

转载 作者:行者123 更新时间:2023-11-29 13:50:41 26 4
gpt4 key购买 nike

我正在尝试通过 Python 在 Postgres 中创建一个函数。我与 postgres 数据库的连接使用 Psycopg 2 并在其他实例中成功连接。代码:

pg_cursor.execute('CREATE OR REPLACE FUNCTION %s.fix_geometry(geometry) \
RETURNS SETOF geometry AS\
$BODY$\
SELECT geom the_geom\
FROM \
(\
SELECT (st_dump(st_buffer(st_snaptogrid(st_makevalid(the_geom), 0.5), 0))).geom\
FROM (SELECT geom the_geom FROM st_dump(st_snaptogrid($1, 0.5))) a\
) b\
WHERE geometrytype(geom) = \'POLYGON\' AND st_area(geom) >= 0.01;\

$BODY$\
LANGUAGE sql VOLATILE\
COST 100\
ROWS 1000;' %(schema)) #line 84

pg_connection.commit()
pg_cursor.execute('ALTER FUNCTION %s.fix_geometry(geometry) OWNER TO analysis' % (schema))
pg_connection.commit()

我得到一个错误:

line 84, in
ROWS 1000;' %(schema))
ProgrammingError: type geometry does not exist

当我在 pgAdmin 中运行代码时,它会成功执行。我错过了什么?

Python 2.7,Postgres 9.3

最佳答案

事实证明,错误就在提供的代码片段之外。 Postgre 在公共(public)模式中有几何类型,当我为这段代码定义搜索路径时,我只定义了我工作的模式;不包括公众。所以....

pg_cursor.execute('set search_path =  %s, public' % (schema))
pg_connection.commit()

关于python - 在 Python 中创建 Postgres 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41602531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com