为什么我可以使用 tsql 而不是 pymssql 连接到 Azure MS SQL?

Why can I connect to Azure MS SQL with tsql but not pymssql?

我今天在哪里:

TDSVER=7.3 tsql -H example.database.windows.net -U me -D ExampleDB -p 1433 -P notreallymypassword

这不是:

>>> import pymssql
>>> pymssql.connect('example.database.windows.net', user='me', password='notreallymypassword', database='ExampleDB', tds_version='7.3')

失败

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
  File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
  File "_mssql.pyx", line 577, in _mssql.MSSQLConnection.__init__ (_mssql.c:6214)
  File "_mssql.pyx", line 1704, in _mssql._tds_ver_str_to_constant (_mssql.c:18845)
_mssql.MSSQLException: unrecognized tds version: 7.3

好的。嗯,这很……奇怪。所以我回去尝试使用 TDSVER=7.2tsql,这似乎工作正常。

尝试连接 tds_version='7.2' 得到:

Traceback (most recent call last):
  File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
  File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
  File "_mssql.pyx", line 637, in _mssql.MSSQLConnection.__init__ (_mssql.c:6581)
  File "_mssql.pyx", line 1630, in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:17524)
_mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (datawhse.database.
windows.net:1433)\n')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (datawhse.database.windo
ws.net:1433)\n')

那么,是什么原因呢?

更新 1: pyodbc 无法连接:

conn = pyodbc.connect('SERVER=example.database.windows.net;Driver=FreeTDS;UID=me@example.database.windows.net;PWD=notmyrealpassword;'
, ansi=True)

我的~/.odbcinst.ini:

[FreeTDS]
Description     = MS SQL driver
Driver          = /usr/lib64/libtdsodbc.so.0
Driver64        = /usr/lib64/libtdsodbc.so.0
Setup           = /usr/lib64/libtdsS.so.0
Setup64         = /usr/lib64/libtdsS.so.0
UsageCount      = 1
CPTimeout       =
CPReuse         =
Trace           = Yes

这个输出:

⚘ odbcinst -j
unixODBC 2.3.1
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/me/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

您的连接字符串看起来不正确。它应该是这样的:

pymssql.connect(server='example.database.windows.net', user='me@example', password='notreallymypassword', database='ExampleDB')

请注意,在调用 connect() 的示例中,您缺少 server= 参数;您只有完整的服务器名称。

看起来 Gord 是对的:问题是 pymssql wheel 没有 SSL 绑定.

我卸载了它:

python -m pip uninstall pymssql

然后从源安装它:

python -m pip install --no-binary pymssql pymssql

这需要我安装一些依赖项。但现在我可以连接

pymssql.connect('example.database.windows.net',
                user='me', 
                password='notreallymypassword', 
                database='ExampleDB',
                tds_version='7.2')