Pyodbc giving exception with older version of sql driver but failing with latest version

浪子不回头ぞ 提交于 2021-01-29 09:26:08

问题


I am trying to connect a SQL server using pyodbc. In that server SSL3.0 is disabled and TLS 1.1 and 1.2 is enabled.

When I am using the oldest driver {SQL Server}. I am getting the below exception handled.

pyodbc.connect('DRIVER={SQL Server};SERVER=XX;DATABASE=XX;UID=XX;PWD=XX;'
('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SSL Security error (18) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (SECDoClientHandshake()). (772)')

However, as a workaround when I used the latest odbc driver {ODBC Driver 17 for SQL Server}

pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=XX;DATABASE=XX;UID=XX;PWD=XX;'

The python script is crashing even with a try catch block using baseclass Exception to handle the exception.

Below is the details of crash:

 Problem Event Name:    APPCRASH
  Application Name: pythonw.exe
  Application Version:  3.8.150.1013
  Application Timestamp:    5da4cb37
  Fault Module Name:    msodbcsql17.dll
  Fault Module Version: 2017.175.2.1
  Fault Module Timestamp:   5e5e976f
  Exception Code:   c0000005
  Exception Offset: 00024005
  OS Version:   6.1.7601.2.1.0.272.7
  Locale ID:    2057
  Additional Information 1: a7aa
  Additional Information 2: a7aa91f17ea749d42a4de3b390fa5b3d
  Additional Information 3: a7aa
  Additional Information 4: a7aa91f17ea749d42a4de3b390fa5b3d

I also enabled trace through ODBC Data source Administration and found the below trace:


pythonw.exe -c  a40-1154    ENTER SQLSetEnvAttr 
        SQLHENV             0x00000000
        SQLINTEGER                 201 <SQL_ATTR_CONNECTION_POOLING>
        SQLPOINTER                 2 <SQL_CP_ONE_PER_HENV>
        SQLINTEGER                   4 

pythonw.exe -c  a40-1154    EXIT  SQLSetEnvAttr  with return code 0 (SQL_SUCCESS)
        SQLHENV             0x00000000
        SQLINTEGER                 201 <SQL_ATTR_CONNECTION_POOLING>
        SQLPOINTER                 2 <SQL_CP_ONE_PER_HENV>
        SQLINTEGER                   4 

pythonw.exe -c  a40-1154    ENTER SQLAllocHandle 
        SQLSMALLINT                  1 <SQL_HANDLE_ENV>
        SQLHANDLE           0x00000000
        SQLHANDLE *         0x047FEC6C

pythonw.exe -c  a40-1154    EXIT  SQLAllocHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  1 <SQL_HANDLE_ENV>
        SQLHANDLE           0x00000000
        SQLHANDLE *         0x047FEC6C ( 0x029B1BE0)

pythonw.exe -c  a40-1154    ENTER SQLSetEnvAttr 
        SQLHENV             0x029B1BE0
        SQLINTEGER                 200 <SQL_ATTR_ODBC_VERSION>
        SQLPOINTER                 3 <SQL_OV_ODBC3>
        SQLINTEGER                   4 

pythonw.exe -c  a40-1154    EXIT  SQLSetEnvAttr  with return code 0 (SQL_SUCCESS)
        SQLHENV             0x029B1BE0
        SQLINTEGER                 200 <SQL_ATTR_ODBC_VERSION>
        SQLPOINTER                 3 <SQL_OV_ODBC3>
        SQLINTEGER                   4 

pythonw.exe -c  a40-1154    ENTER SQLAllocHandle 
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x029B1BE0
        SQLHANDLE *         0x0041F4B0

pythonw.exe -c  a40-1154    EXIT  SQLAllocHandle  with return code 0 (SQL_SUCCESS)
        SQLSMALLINT                  2 <SQL_HANDLE_DBC>
        SQLHANDLE           0x029B1BE0
        SQLHANDLE *         0x0041F4B0 ( 0x029B1C58)

pythonw.exe -c  a40-1154    ENTER SQLDriverConnectW 
        HDBC                0x029B1C58
        HWND                0x00000000
        WCHAR *             0x04848B34 [      -3] "******\ 0"
        SWORD                       -3 
        WCHAR *             0x04848B34 
        SWORD                       -3 
        SWORD *             0x00000000
        UWORD                        0 <SQL_DRIVER_NOPROMPT>

When run with python.exe through cmd. No traces found in cmd but again that crashed with below details:

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: python.exe
  Application Version:  3.8.150.1013
  Application Timestamp:    5da4cb35
  Fault Module Name:    msodbcsql17.dll
  Fault Module Version: 2017.175.2.1
  Fault Module Timestamp:   5e5e976f
  Exception Code:   c0000005
  Exception Offset: 00024005
  OS Version:   6.1.7601.2.1.0.272.7
  Locale ID:    2057
  Additional Information 1: a7aa
  Additional Information 2: a7aa91f17ea749d42a4de3b390fa5b3d
  Additional Information 3: a7aa
  Additional Information 4: a7aa91f17ea749d42a4de3b390fa5b3d


回答1:


Try changing the driver parameter from "SQL Server" to "SQL Server Native Client 11.0". This led to a successful connection for me.

import pyodbc
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER=<your server name>;Trusted_Connection=yes')

In my case, the "SQL Server" driver parameter was working on some machines but not others. I'm doubtful that it was TLS disablement issue on the server side, since the other machines could connect with pyodbc. I am curious if someone knows what else may be the cause.



来源:https://stackoverflow.com/questions/62554776/pyodbc-giving-exception-with-older-version-of-sql-driver-but-failing-with-latest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!