如何使用Python jaydebiapi cursor.executemany()将地理数据插入SQL Server?

如何使用Python jaydebiapi cursor.executemany()将地理数据插入SQL Server?,python,sql-server,sqlgeography,jaydebeapi,Python,Sql Server,Sqlgeography,Jaydebeapi,我确实使用了cursor.execute()将数据插入SQL Server 以下是我的代码片段: insert_str = """INSERT INTO [test].[DBO].[SPATIAL] VALUES({},{});""" geography = "geography::STGeomFromText('LINESTRING(-95.323167 29.985500, -95.323333 29.985500)', 4326)" cursor.execute(insert_str.

我确实使用了
cursor.execute()
将数据插入SQL Server

以下是我的代码片段:

insert_str = """INSERT INTO [test].[DBO].[SPATIAL] VALUES({},{});"""

geography = "geography::STGeomFromText('LINESTRING(-95.323167 29.985500, -95.323333 29.985500)', 4326)"

cursor.execute(insert_str.format(123, geography))
但是当我对下面的代码片段使用
cursor.executemany()
时,它不起作用:

insert_str = """INSERT INTO [test].[DBO].[SPATIAL] VALUES(?,?);"""

geography = "geography::STGeomFromText('LINESTRING(-95.323167 29.985500, -95.323333 29.985500)', 4326)"

cursor.executemany(insert_str, [(122, geography)])
这是回溯日志:

Traceback (most recent call last):
  File "spatial_point.py", line 35, in <module>
    run()
  File "spatial_point.py", line 33, in run
    cursor.executemany(insert_str, [(122, geography)])
  File "/root/anaconda3/lib/python3.7/site-packages/jaydebeapi/__init__.py", line 518, in executemany
    update_counts = self._prep.executeBatch()
jpype._jexception.BatchUpdateExceptionPyRaisable: java.sql.BatchUpdateException: A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":
System.FormatException: 24114: The label geography::STGeomFro in the input well-known text (WKT) is not valid. Valid labels are POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION, CIRCULARSTRING, COMPOUNDCURVE, CURVEPOLYGON and FULLGLOBE (geography Data Type only).
System.FormatException:
   at Microsoft.SqlServer.Types.OpenGisTypes.ParseLabel(String input)
   at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type)
   at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.ParseText(OpenGisType type, SqlChars taggedText, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.Parse(SqlString s)
回溯(最近一次呼叫最后一次):
文件“space_point.py”,第35行,在
运行()
文件“spatial_point.py”,第33行,运行中
cursor.executemany(insert_str,[(122,地理)])
文件“/root/anaconda3/lib/python3.7/site packages/jaydebeapi/_init__.py”,第518行,在executemany中
更新计数=self.\u prep.executeBatch()
jpype.\u jexception.batchUpdateExceptionPraisable:java.sql.BatchUpdateException:在执行用户定义例程或聚合“地理”期间发生.NET Framework错误:
System.FormatException:24114:输入已知文本(WKT)中的标签geography::STGeomFro无效。有效标签包括点、线串、多边形、多点、多线串、多多边形、GEOMETRYCOLLECTION、CIRCULARSTRING、COMPOUNDCURVE、CURVEPOLYGON和FULLGLOBE(仅限地理数据类型)。
System.FormatException:
位于Microsoft.SqlServer.Types.OpenGisTypes.ParseLabel(字符串输入)
位于Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType类型)
位于Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType类型,Int32 srid)
位于Microsoft.SqlServer.Types.SqlGeography.ParseText(OpenGisType类型,SqlChars taggedText,Int32 srid)
位于Microsoft.SqlServer.Types.sqlgeographyfromtext(OpenGisType类型,SqlChars taggedText,Int32 srid)
位于Microsoft.SqlServer.Types.SqlGeography.Parse(SqlString s)

有人知道这里出了什么问题吗?感谢

第一种方法只是将文本
插入到[test].[DBO].[SPATIAL]值({},{})中格式化
并将位置参数替换为值
123
“geography::STGeomFromText('LINESTRING(-95.323167 29.985500,-95.323333 29.985500)”,4326)”

第二种方法使用占位符,因此语句应该不同。您需要将地理实例的WKT表示形式和空间引用ID作为
geography::stgeomefromtext
调用的参数传递:

insert_str = """INSERT INTO [test].[DBO].[SPATIAL] VALUES(?, geography::STGeomFromText(?, ?);"""
cursor.executemany(insert_str, [(122, 'LINESTRING(-95.323167 29.985500, -95.323333 29.985500)', 4326)])

第一种方法只是将文本
INSERT格式化为[test].[DBO].[SPATIAL]值({},{})
并将位置参数替换为值
123
“geography::STGeomFromText('LINESTRING(-95.323167 29.985500,-95.323333 29.985500)”,4326)”

第二种方法使用占位符,因此语句应该不同。您需要将地理实例的WKT表示形式和空间引用ID作为
geography::stgeomefromtext
调用的参数传递:

insert_str = """INSERT INTO [test].[DBO].[SPATIAL] VALUES(?, geography::STGeomFromText(?, ?);"""
cursor.executemany(insert_str, [(122, 'LINESTRING(-95.323167 29.985500, -95.323333 29.985500)', 4326)])