Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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
如何将点数据从python导入postreSQL数据库?_Python_Sql_Insert_Geometry_Point - Fatal编程技术网

如何将点数据从python导入postreSQL数据库?

如何将点数据从python导入postreSQL数据库?,python,sql,insert,geometry,point,Python,Sql,Insert,Geometry,Point,我有一个数据帧df,其中一列名为“geom”。它由经度、纬度坐标组成: e、 g.点(10.1768627166748 50.1768627166748) 我尝试使用以下命令将这一单列数据导入postreSQL中数据库的相应列 from geoalchemy2 import Geometry df['geom'].to_sql("...", engine, schema='...', if_exists='append', index=False, dtype={'geom'

我有一个数据帧
df
,其中一列名为“geom”。它由经度、纬度坐标组成:

e、 g.
点(10.1768627166748 50.1768627166748)

我尝试使用以下命令将这一单列数据导入postreSQL中数据库的相应列

from geoalchemy2 import Geometry
df['geom'].to_sql("...", engine, schema='...', if_exists='append', index=False, dtype={'geom': Geometry(geometry_type='POINT', srid=4326)})
但是,我得到了这个错误:

IntegrityError: (psycopg2.errors.NotNullViolation) null value in column "id" violates not-null constraint
请注意,
id
是数据库的主键。有什么想法吗?提前感谢。

您需要在数据框中添加一个“id”列并为其指定一个唯一值,目前您正试图在pk字段中插入null:

# add column
df['id'] = 1234 
# Observe the result 
df
# insert entire df (not just geom column)
df.to_sql("...", engin ...

确保插入整个数据帧(而不仅仅是geom列)。

谢谢@PetArbiter的回复。当我实际尝试导入整个df时,我得到了以下错误:ProgrammingError:(psycopg2.ProgrammingError)无法调整类型'Point'有趣,因此psycopg2不喜欢Point??我现在没有安装Postgres来重新创建您的问题,但是我可以想到一些解决方法:使用标识列创建id字段(约束生成为identity),这样您就不必插入id(它将自动递增一个整数),或者可能将点字段重新拆分为lat/long列并插入df(我知道不太理想)。