【问题标题】:How to insert geography data to SQL Server with Python jaydebiapi cursor.executemany()?如何使用 Python jaydebiapi cursor.executemany() 将地理数据插入 SQL Server?
【发布时间】:2020-08-18 23:22:41
【问题描述】:

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

这是我的代码 sn-p:

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() 和下面的代码sn-p 时,它不起作用:

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)

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

【问题讨论】:

    标签: python sql-server sqlgeography jaydebeapi


    【解决方案1】:

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

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

    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)])
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2012-09-20
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多