PostGIS query failing due to escape string?

和自甴很熟 提交于 2020-01-16 07:19:27

问题


I have this Postgres/PostGIS query:

UPDATE raw.geocoding
SET the_geom = ST_Transform(ST_GeomFromText('POINT(((E'-96.6864379495382')::float8) ((E'32.792527154088')::float8))', 4326),3081)
WHERE id=((10793455)::int4)

When I run it, I get this error:

ERROR:  syntax error at or near "')::float8) ((E'"
LINE 2: ...sform(ST_GeomFromText('POINT(((E'-96.6864379495382')::float8...
                                                             ^

********** Error **********

ERROR: syntax error at or near "')::float8) ((E'"
SQL state: 42601
Character: 94

I'm scratching my head because PostGIS doesn't have a problem with escaped data (for example), and the query was generated from npgsql based on this parameterized query:

UPDATE raw.geocoding
SET the_geom = ST_Transform(ST_GeomFromText('POINT(:longitude :latitude)', 4326),3081)
WHERE id=:id

I am running Postgres 9.1.5 and PostGIS 2.0.1.


回答1:


The error results from unescaped single quotes in the string. The standard way is to double them:

UPDATE raw.geocoding
SET    the_geom = ST_Transform(ST_GeomFromText(
               'POINT(((E''-96.6864379495382'')::float8)
                     ((E''32.792527154088'')::float8))', 4326),3081)
WHERE  id=((10793455)::int4)

This fixes the string literal, but you have more errors.
Like @Paul hinted in a comment, ST_GeomFromText() expects the geometry WKT POINT(0 0). The explicit cast to float8 makes it look like you are trying to enter the Postgres function point() (had me confused at first). Simplify to:

UPDATE raw.geocoding
SET    the_geom = ST_Transform(ST_GeomFromText(
                  $$POINT(96.6864379495382 32.792527154088)$$, 4326), 3081)
WHERE  id = 10793455

Note also, how I use dollar quoting in the second example to avoid having to escape single quotes altogether. As there aren't any single quotes left in your string literal after fixing the syntax, you might as well use single quotes again. Your parametrized query:

UPDATE raw.geocoding
SET    the_geom = ST_Transform(ST_GeomFromText(
                  $$POINT(:longitude :latitude)$$::geometry, 4326), 3081)
WHERE  id = :id

You can add a cast to geometry to make it clear, like @Paul advises in his comment. But it works without explicit cast, too.



来源:https://stackoverflow.com/questions/12398904/postgis-query-failing-due-to-escape-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!