python pyodbc SQL Server Native Client 11.0 无法返回几何列 [英] python pyodbc SQL Server Native Client 11.0 cannot return geometry column

查看:47
本文介绍了python pyodbc SQL Server Native Client 11.0 无法返回几何列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 python 2.7 安装了 SQL Server Native Client 11.0 和 pyodbc.我能够在 python 中正确设置连接

I have SQL Server Native Client 11.0 and pyodbc installed using python 2.7. I am able to set the connection correctly within python

import pyodbc
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER=b;DATABASE=b;UID=b;PWD=b')
cur=conn.cursor()
query=cur.execute('''select top 1 * from table''')
for x in query.fetchall():
    print x

Traceback (most recent call last):
  File "<module1>", line 8, in <module>
ProgrammingError: ('ODBC SQL type -151 is not yet supported.  column-index=40  type=-151', 'HY106')

如果我这样做,我会得到同样的错误

if i do, i get the same error

for x in query:
   print x

如何返回查询?

如果我只是打印查询,我会在 0x02B74AD8 处得到 pyodbc.Cursor 对象

if I just print query I get pyodbc.Cursor object at 0x02B74AD8

可能是它无法读取的列类型,此表中有一个点几何.

could it be column type it cannot read, there is a point geometry in this table.

更新

问题在于点几何.当我取出几何图形时,我可以返回光标包含的内容.然而,当几何列在那里时,它总是通过这个错误.我假设 pyodbc 不支持几何对象......作为一种解决方法,我可以转换为 WKT

the problem is with the point geometry. when I take out the geometry I can return what the cursor contains. however when the geometry column is in there it always throughs this error. I am assuming the pyodbc does not have support for geometric objects... as a work around I can convert to WKT

推荐答案

基于 GitHub issue 这里,下面是输出转换器函数 似乎可以解决问题:

Based on the GitHub issue here, the following output converter function seems to do the trick:

def unpack_geometry(raw_bytes):
    # adapted from SSCLRT information at
    #   https://docs.microsoft.com/en-us/openspecs/sql_server_protocols/ms-ssclrt/dc988cb6-4812-4ec6-91cd-cce329f6ecda
    tup = struct.unpack('<i2b3d', raw_bytes)
    # tup contains: (unknown, Version, Serialization_Properties, X, Y, SRID)
    return tup[3], tup[4], tup[5]

# ...

cnxn.add_output_converter(-151, unpack_geometry)
crsr.execute("SELECT CAST('POINT(-79.528874 43.648533 12345)' AS geometry)")
print(crsr.fetchval())  # (-79.528874, 43.648533, 12345.0)

这篇关于python pyodbc SQL Server Native Client 11.0 无法返回几何列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆