尚不支持ODBC SQL类型

编程入门 行业动态 更新时间:2024-10-25 20:26:16
本文介绍了尚不支持ODBC SQL类型-155的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遵循此链接查询Azure数据库.

I follow this link to query Azure database.

import pyodbc server = 'your_server.database.windows' database = 'your_database' username = 'your_username' password = 'your_password' driver= '{ODBC Driver 13 for SQL Server}' cnxn = pyodbc.connect('DRIVER='+driver+';PORT=1433;SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+ password) cursor = cnxn.cursor() cursor.execute("SELECT * FROM FinancialRecord where deleted=0") row = cursor.fetchone() while row: print (str(row[0]) + " " + str(row[1])) row = cursor.fetchone()

运行上面的代码时,它显示错误.

When I run the code above, it show the error.

回溯(最近通话最近): 在第10行的文件"sqltest.py"中 行= cursor.fetchone() pyodbc.ProgrammingError :('ODBC SQL类型-155尚不支持.column-index = 2 type = -155','HY106')

Traceback (most recent call last): File "sqltest.py", line 10, in row = cursor.fetchone() pyodbc.ProgrammingError: ('ODBC SQL type -155 is not yet supported. column-index=2 type=-155', 'HY106')

我是Azure的新手.有人可以帮忙吗?

I am new to Azure. Anyone can help?

推荐答案

pyodbc支持输出转换器函数,当数据库返回pyodbc本机不支持的SQL类型时,我们可以使用它们.上面链接的Wiki页面上的示例将执行客户端转换,类似于通过CAST到服务器上的[N] VARCHAR进行的转换:

pyodbc supports Output Converter functions that we can use when a database returns an SQL type that pyodbc does not support natively. The example on the Wiki page linked above will perform a client-side conversion similar to what would be achieved by a CAST to [N]VARCHAR on the server:

import struct import pyodbc conn = pyodbc.connect("DSN=myDb") def handle_datetimeoffset(dto_value): # ref: github/mkleehammer/pyodbc/issues/134#issuecomment-281739794 tup = struct.unpack("<6hI2h", dto_value) # e.g., (2017, 3, 16, 10, 35, 18, 0, -6, 0) tweaked = [tup[i] // 100 if i == 6 else tup[i] for i in range(len(tup))] return "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:07d} {:+03d}:{:02d}".format(*tweaked) crsr = conn.cursor() # create test data crsr.execute("CREATE TABLE #dto_test (id INT PRIMARY KEY, dto_col DATETIMEOFFSET)") crsr.execute("INSERT INTO #dto_test (id, dto_col) VALUES (1, '2017-03-16 10:35:18 -06:00')") conn.add_output_converter(-155, handle_datetimeoffset) value = crsr.execute("SELECT dto_col FROM #dto_test WHERE id=1").fetchval() print(value) crsr.close() conn.close()

可打印

2017-03-16 10:35:18.0000000 -06:00

更多推荐

尚不支持ODBC SQL类型

本文发布于:2023-11-15 04:38:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:尚不   类型   ODBC   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!