Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 使用Python SQLAlchemy将POSTGIS数据类型插入PostgresDB_Postgresql_Python 3.x_Sqlalchemy_Postgis_Geoalchemy - Fatal编程技术网

Postgresql 使用Python SQLAlchemy将POSTGIS数据类型插入PostgresDB

Postgresql 使用Python SQLAlchemy将POSTGIS数据类型插入PostgresDB,postgresql,python-3.x,sqlalchemy,postgis,geoalchemy,Postgresql,Python 3.x,Sqlalchemy,Postgis,Geoalchemy,我试图使用SQLalchemy将Linestring、Polygon和其他数据类型的POSTGIS值插入postgres DB,但遇到以下错误: 这是使用postgres db命令创建的表的架构: Table "public.test" Column | Type | Modifiers --------------+---------------------------+----------- nam

我试图使用SQLalchemy将Linestring、Polygon和其他数据类型的POSTGIS值插入postgres DB,但遇到以下错误:

这是使用postgres db命令创建的表的架构:

                 Table "public.test"
    Column    |           Type            | Modifiers 
--------------+---------------------------+-----------
 name         | text                      | 
 wkb_geometry | geometry(LineString,4326) | 
postgres表的架构已使用DDL语句创建

我已尝试使用psycopg2从xml插入gml值,并使用以下命令成功插入该值:

wkb_geometry_value = """<gml:LineString xmlns:gml="http://www.opengis.net/gml" xmlns="urn:hera:xml:ns:DataTypes:2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hera-drex="urn:hera:xml:ns:exchange:2.0" xmlns:hera-sgftad="urn:hera:xml:ns:sft:2.0" xmlns:hera-rifdt="urn:hera:xml:nsftypes:2.0" xmlns:wfs="http://www.opengis.org/wfs: " xmlns:gb="http://www.opengis.org/gb" xmlns:georss="http://www.opengis.netgeorss"><gml:posList>34.216 -210.54 34.46 -119.4 34.65 -139.84</gml:posList>\</gml:LineString>"""

sql = INSERT INTO test('name','wkb_geometry)VALUES('BOB', ST_SetSRID(ST_GeomFromGML('{}',4326)))
cursor.execute(sql)
输出==>内部错误:(psycopg2.InternalError)分析错误-无效几何体

如何使用SQLalchemy和geoalchemy将GIS数据类型插入PostgresDB

任何帮助都将不胜感激 谢谢
I

首先:永远不要使用字符串格式将值传递给SQL查询。你迟早会遇到麻烦的,“麻烦”是从“我的查询失败”到“我被拥有”。使用占位符。对于SQLAlchemy read,您的wkb_geometry_值看起来不像,但是,请使用,就像您对普通psycopg2所做的那样。@IljaEverilä-您是否有一个示例来演示在不使用本机包装psycopg2的情况下使用SQLAlchemy将gml标记插入postgresdb?没有,您以前使用的查询将按原样工作。您的SQLAlchemy版本正在尝试对GML数据使用
ST_GeomFromEWKT()
,而您的原始psycopg2版本使用的是正确的
ST_GeomFromGML(…)
调用。因此,投票结果是“非常简单的打字错误/不重复”。
import sqlalchemy                                                           
from sqlalchemy import create_engine            
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy import inspect 

sql = """INSERT INTO test (name, wkb_geometry) VALUES ('{}', ST_GeomFromEWKT('{}'))""".format('BOB', wkb_geometry_value)
engine = create_engine('postgresql://username:password@localhost/dbname')

 with engine.connect() as con:
    rs = con.execute(sql)