简体   繁体   English

pymssql:有时只能连接数据库

[英]pymssql: Connection to the database only works sometimes

I'm trying to connect to Azure SQL server using Python's pymssql. 我正在尝试使用Python的pymssql连接到Azure SQL服务器。 The problem is that the following script works but only sometimes, the other times I get this error: 问题是以下脚本有效,但有时只有,其他时候我收到此错误:

_mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\\nAdaptive Server connection failed\\n') _mssql.MSSQLDatabaseException:(20002,b'DB-Lib错误消息20002,严重性9:\\ nAdaptive Server连接失败\\ n')

This is the script I'm using: 这是我正在使用的脚本:

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()

It would help me greatly if someone can tell me why this above script works only sometimes and rest of the times I get the "Adaptive Server connection failed" error. 如果有人能告诉我为什么上面的脚本有时只能工作而且剩下的时间我得到“Adaptive Server连接失败”错误,这对我有很大的帮助。

I reviewed these SO old threads 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? 我查看了这些旧的线程从服务器读取的尝试从tsql连接到sql-azure失败了 什么是TDS协议版本8.0,为什么要使用它? . The issue seems to be caused by using the wrong version of FreeTDS. 这个问题似乎是由使用错误版本的FreeTDS引起的。

I found the key at the page of FreeTDS offical website http://www.freetds.org/faq.html#Does.FreeTDS.support.Microsoft.servers . 我在FreeTDS官方网站http://www.freetds.org/faq.html#Does.FreeTDS.support.Microsoft.servers页面找到了密钥。

There is a table of versions of the TDS protocol by product http://www.freetds.org/userguide/choosingtdsprotocol.htm . 按产品http://www.freetds.org/userguide/choosingtdsprotocol.htm有一个TDS协议版本表。

在此输入图像描述

FreeTDS will alias this version to 7.1 for backwards compatibility reasons, but this should be avoided due to future compatibility concerns. 出于向后兼容性原因,FreeTDS会将此版本的别名设为7.1,但由于未来的兼容性问题,应该避免这种情况。 See note below on obsolete versions. 请参阅下面有关过时版本的说明。

If you use pymssql with FreeTDS on linux, I think you need to check the configuration file "freetds.conf" at the path /etc/freetds/. 如果你在Linux上使用带有FreeTDS的pymssql,我认为你需要在路径/ etc / freetds /上检查配置文件“freetds.conf”。

This is my configuration for Azure SQL Server below: 这是我在下面的Azure SQL Server配置:

# A typical Microsoft server
[egServer70]
        host = <database_name>.database.windows.net
        port = 1433
        tds version = 7.3

You can try to test the connection to Azure SQL server by using freetds tool 'tsql' to command ' tsql -H <database_name>.database.windows.net -U Username -D DatabaseName -p 1433 -P Password ' . 您可以尝试使用freetds工具'tsql'命令' tsql -H <database_name>.database.windows.net -U Username -D DatabaseName -p 1433 -P Password '来测试与Azure SQL服务器的连接。

Best Regards. 最好的祝福。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM