使用 pymssql 连接到 SQL Server 实例

发布于 2025-01-02 19:00:15 字数 1589 浏览 1 评论 0原文

我正在尝试使用 pymssql(版本 2.0.0b1-dev-20111019,使用 Python 2.7.1)从 Windows 机器连接到 SQL Server 实例。我已经从控制台尝试了最基本的方法:

import pymssql
c = pymssql.connect(host = r'servername\instance',
                    user = 'username',
                    password = 'userpassword')

对此,我得到了非常有用的错误:InterfaceError:由于未知原因,与数据库的连接失败。

我有相当的信心该连接信息是正确的,因为当我使用 adodbapi 并使用以下命令时它可以工作:

import adodbapi
c = adodbapi.connect(r'Provider=sqloledb;Data Source=servername\instance;User ID=username;password=userpassword;'
c.close

我尝试将端口号添加到主机参数中,得到相同的结果。有人对如何解决这个问题有建议吗?


顺便说一句,我已阅读“无法连接到 SQL Server”处的响应通过 pymssql”。 OP 最终通过正确配置 FreeTDS 解决了他的问题,据我所知,Windows 上的 pymssql 不使用 FreeTDS。


根据 @cha0site 的建议,我尝试仅使用主机名,而不是主机名和实例。这导致了相同的错误,但似乎需要更长的时间才能生成错误(尽管回溯仍然指示同一行)。我指定实例的原因是,除非指定实例,否则我无法使用 SSMS 进行连接,因此我认为其他连接也需要它。


我现在也尝试了 pymssql.connect(host='servername', user='username', password='userpassword', database='instance') 得到了相同的结果(基于 @Sid 的评论)。根据 pymssql 文档,我相信数据库 参数用于指定用户要连接的初始数据库,而不是实例。

需要澄清的是,“实例”是 SQL Server 安装期间提供的名称,而不是该安装中的数据库。我认为 pymssql 可能不支持此表示法,因此我将考虑重新配置 SQL Server 实例,以便不需要它。


我现在已将 SQL Server 重新安装为默认实例,而不是命名实例,这样我就可以在不指定实例名称的情况下进行连接。 adodbapi 仍然可以工作(没有 /instance),但 pymssql 仍然返回相同的错误。我还从新下载的存档中删除并重新安装了 pymssql(仍然是相同的版本)。

I'm attempting to connect to a SQL Server instance from a Windows box using pymssql (version 2.0.0b1-dev-20111019 with Python 2.7.1). I've tried the most basic approach from the console:

import pymssql
c = pymssql.connect(host = r'servername\instance',
                    user = 'username',
                    password = 'userpassword')

In response to this, I get the very helpful error: InterfaceError: Connection to the database failed for an unknown reason.

I am reasonably confident that the connection information is correct, as it works when I use adodbapi, with the following commands:

import adodbapi
c = adodbapi.connect(r'Provider=sqloledb;Data Source=servername\instance;User ID=username;password=userpassword;'
c.close

I've tried adding the port number to the host parameter, with the same result. Does anyone have a suggestion on how to go about resolving this issue?


Incidentally, I've read the responses at "Unable to connect to SQL Server via pymssql". The OP eventually resolved his issue by correctly configuring FreeTDS, which, from what I can tell, is not used by pymssql on Windows.


Based on @cha0site's recommendation, I have tried using just the hostname, rather than the hostname and instance. This resulted in the same error, but it seemed to take longer to generate the error (though the traceback still indicates the same line). The reason I have been specifying the instance is that I was not able to connect using SSMS unless I specified the instance, so I assumed that it would be necessary for other connections.


I've now also tried pymssql.connect(host='servername', user='username', password='userpassword', database='instance') with the same result (based on @Sid's comment). Based on the pymssql documentation, I believe the database parameter is used to specify the initial database that the user is to be connected to, rather than the instance.

Just to clarify, "instance" is the name provided during installation of SQL Server, not a database within that installation. It occurs to me that it's possible that pymssql does not support this notation, so I will look into re-configuring the SQL Server instance so that it is not required.


I've now re-installed SQL Server as a default instance, rather than a named instance, which allows me to connect without specifying the instance name. adodbapi still works (without /instance), but pymssql still returns the same error. I've also removed and re-installed pymssql from a freshly downloaded archive (still the same version).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2025-01-09 19:00:15

要指定 host=servername\instanceserver=servername\instanceSQL Server Browser< /em> 服务必须位于 SQL Server 计算机上。

To specify host=servername\instance or server=servername\instance, the SQL Server Browser service must be on the SQL Server machine.

浅暮の光 2025-01-09 19:00:15

这里可以在我的工作空间上工作
pymssql版本:2.2.8
python 版本:3.11

conn = pymssql.connect(host=r'10.0.1.29:60001\sql2019', user='sa', password='sa', charset='UTF-8', atabase=r'database1', tds_version=r'7.0')

如果您使用 pymssql 2.2.8,则 tds_version 应设置为 7.0。您可以尝试其他 tds_version(7.4,7.3,7.2,7.1,7.0...) 如果您仍然遇到问题,

您可以添加附加版本信息和调试代码

print('pymssql version: ', pymssql.__version__)
print('get_dbversion: ', pymssql.get_dbversion())
print('version_info: ', pymssql.version_info())
os.environ['TDSDUMP'] = 'stdout'

Here can work on my workspace
pymssql version: 2.2.8
python verison: 3.11

conn = pymssql.connect(host=r'10.0.1.29:60001\sql2019', user='sa', password='sa', charset='UTF-8', atabase=r'database1', tds_version=r'7.0')

the tds_version should be set 7.0 if you used pymssql 2.2.8. you can have a try other tds_version(7.4,7.3,7.2,7.1,7.0...) if you still encountered issue

you can add addtional version informaiton and debug code

print('pymssql version: ', pymssql.__version__)
print('get_dbversion: ', pymssql.get_dbversion())
print('version_info: ', pymssql.version_info())
os.environ['TDSDUMP'] = 'stdout'
凉城凉梦凉人心 2025-01-09 19:00:15

检查你的 freetds.conf 文件并查看是否设置了端口 1219,然后再次检查连接:

DB = pymssql.connect(host='DB',user='youruser',password='yourpwd',database='yourDBname')

编辑:我的 freetds.conf 文件 Python 的示例:

host = 'IP'
port = 1219

Check your freetds.conf file and see if you have set the port 1219., then check again the connection:

DB = pymssql.connect(host='DB',user='youruser',password='yourpwd',database='yourDBname')

Edit: example of my freetds.conf file Python:

host = 'IP'
port = 1219
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文