我想在Python中使用psycopg2模块执行INSERT运算符。我可以在psycopg2(Python)的“INSERT”代码中使用PostgreSQL/PostGIS函数吗?
的代码是:
cursor.execute('INSERT INTO tb_places (id_street, geom, number, name, first_year, source, id_user, date) VALUES
(22, ST_GeomFromText("POINT(-518.944 -2698.2069)", 4326), 34, TEST_1, 1950, Almanak96, 6, 2017-08-01)')
当我使用串联,生成INSERT字符串,它的工作原理,但我知道这是一个坏主意。所以我试图使用其他替代方法来避免SQL注入。
我试了一下:
cursor.execute('INSERT INTO tb_places (id_street, geom, number, name, first_year, source, id_user, date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
[22, "ST_GeomFromText('POINT(-518.944 -2698.2069)', 4326)", 34, 'TEST_1', 1931, 'Almanak96', 6, '2017-08-01'])
但我得到这个错误:
psycopg2.InternalError: parse error - invalid geometry
LINE 1: ...e, first_year, source, id_user, date) VALUES (22, 'ST_GeomFr...
^
HINT: "ST" <-- parse error at position 2 within geometry
基本上,问题是,我无法通过SQL函数的字符串时,我想插入一些。
有人知道我能做什么吗?
怎么第一个查询甚至工作?向我们展示串联版本未填充的值。什么是'ST_GeomFromText()'?那是Python还是Postgres方法?即使Postgres,Python也会因为无法找到函数而发生错误。 – Parfait