我有SQL Server本地客户端11.0,并且使用Python 2.7安装了PYODBC。我能够在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')
如果我这样做,我会得到相同的错误
for x in query:
print x
如何返回查询?
如果我只打印查询,我会得到pyodbc.Cursor object at 0x02B74AD8
可能是无法读取的列类型,该表中有一个点几何形状。
update
问题是点几何形状。当我取出几何形状时,我可以返回光标所包含的内容。但是,当几何列在其中时,它总是会通过此错误。我假设pyodbc不支持几何对象...作为围绕的工作,我可以转换为wkt
基于此处的github问题,以下输出转换器函数似乎可以解决:
def unpack_geometry(raw_bytes):
# adapted from SSCLRT information at
# https://learn.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)