pymssql:有时只能连接到数据库
pymssql: Connection to the database only works sometimes
我正在尝试使用 Python 的 pymssql 连接到 Azure SQL 服务器。问题是以下脚本有效,但有时会出现此错误:
_mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')
这是我正在使用的脚本:
import pymssql
conn = pymssql.connect(server='x', user='x', password='x', database='x')
cursor = conn.cursor()
cursor.execute('SELECT * FROM customers');
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]) + " " + str(row[2]))
row = cursor.fetchone()
如果有人能告诉我为什么上面的脚本有时只工作而其他时候我得到 "Adaptive Server connection failed" 错误,那将对我有很大帮助。
我查看了这些太旧的线程 Read from the server failed when trying to connect to sql-azure from tsql and What is TDS Protocol Version 8.0 and why should I use it?。
问题似乎是使用了错误版本的 FreeTDS 造成的。
我在FreeTDS官网http://www.freetds.org/faq.html#Does.FreeTDS.support.Microsoft.servers页面找到了密钥。
产品 http://www.freetds.org/userguide/choosingtdsprotocol.htm 有 table 个 TDS 协议版本。
出于向后兼容性的原因,FreeTDS 会将此版本别名为 7.1,但出于将来的兼容性考虑,应避免这样做。请参阅下面有关过时版本的注释。
如果你在 linux 上将 pymssql 与 FreeTDS 一起使用,我认为你需要检查路径 /etc/freetds/.
上的配置文件 "freetds.conf"
这是我的 Azure SQL 下面服务器的配置:
# A typical Microsoft server
[egServer70]
host = <database_name>.database.windows.net
port = 1433
tds version = 7.3
您可以尝试使用 freetds 工具 'tsql' 命令“tsql -H <database_name>.database.windows.net -U Username -D DatabaseName -p 1433 -P Password
”来测试与 Azure SQL 服务器的连接。
此致。
我正在尝试使用 Python 的 pymssql 连接到 Azure SQL 服务器。问题是以下脚本有效,但有时会出现此错误:
_mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')
这是我正在使用的脚本:
import pymssql
conn = pymssql.connect(server='x', user='x', password='x', database='x')
cursor = conn.cursor()
cursor.execute('SELECT * FROM customers');
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]) + " " + str(row[2]))
row = cursor.fetchone()
如果有人能告诉我为什么上面的脚本有时只工作而其他时候我得到 "Adaptive Server connection failed" 错误,那将对我有很大帮助。
我查看了这些太旧的线程 Read from the server failed when trying to connect to sql-azure from tsql and What is TDS Protocol Version 8.0 and why should I use it?。 问题似乎是使用了错误版本的 FreeTDS 造成的。
我在FreeTDS官网http://www.freetds.org/faq.html#Does.FreeTDS.support.Microsoft.servers页面找到了密钥。
产品 http://www.freetds.org/userguide/choosingtdsprotocol.htm 有 table 个 TDS 协议版本。
出于向后兼容性的原因,FreeTDS 会将此版本别名为 7.1,但出于将来的兼容性考虑,应避免这样做。请参阅下面有关过时版本的注释。
如果你在 linux 上将 pymssql 与 FreeTDS 一起使用,我认为你需要检查路径 /etc/freetds/.
上的配置文件 "freetds.conf"这是我的 Azure SQL 下面服务器的配置:
# A typical Microsoft server
[egServer70]
host = <database_name>.database.windows.net
port = 1433
tds version = 7.3
您可以尝试使用 freetds 工具 'tsql' 命令“tsql -H <database_name>.database.windows.net -U Username -D DatabaseName -p 1433 -P Password
”来测试与 Azure SQL 服务器的连接。
此致。