pymssql 连接失败 "collation not recognized"

pymssql connection fails with "collation not recognized"

我尝试使用 python3(3.5.3) 库 pymssql(2.1.3) 连接到 sql 服务器 2012 数据库,但发生了这个错误:

conn = pymssql.connect(host="192.168.xxx.xxx", user="wbs", password="123@123", database="mydb")
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: (4075, b'DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.100.249: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: (4075, b'DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.100.249:1433)\n')

并且在 mssql 错误中有一个日志:

The USE database statement failed because the database collation %.*ls is not recognized by older client drivers. Try upgrading the client operating system or applying a service update to the database client software, or use a different collation. See SQL

默认情况下,pip 从静态链接到 FreeTDS 0.95 的二进制轮 (.whl) 文件安装 pymssql 2.1.3。不幸的是,旧版本的 FreeTDS 不知道如何使用一些不常用的 SQL 服务器排序规则。例如,已知 Persian_100_CI_AI 会导致类似于您在 FreeTDS 0.9x 下描述的错误(参考:here)。

FreeTDS 1.x 对这些归类有更好的支持,因此您可以选择安装最新稳定版本的 FreeTDS(当前为 1.00.51),然后从 [=] 上的最新源构建 pymssql 12=].

或者,您可以尝试将 pyodbc 与当前版本的 Microsoft ODBC 驱动程序一起用于 SQL 服务器。