当我尝试访问数据库时,PYODBC操作错误始终说出无法找到或访问服务器,但Docker显示并运行它

发布于 2025-02-08 11:10:48 字数 1220 浏览 1 评论 0原文

使用Docker,我正在创建SQL Server 2019 Image&我和我目前无法执行我的Python脚本,该脚本访问数据库并在特定表中打印结果。 SQL Server在Docker容器中运行。我正在访问终端中的脚本,这只是我本地目录中的一个Python文件。

这是我的代码:

import pyodbc
connector = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};' 'Server=cbbcb967dacb;' 'Database=testdb;' 'UID=sa;' 'PWD=Password' 'Trusted_Connection=yes;')
connector.execute()

cursor = connector.cursor()
cursor.execute('SELECT * FROM inventory')

for i in cursor:
    print(i)

我得到的错误是

pyodbc.operationalError :('08001','[08001] [Microsoft] [SQL Server的ODBC驱动程序18]名为Pipes Provider:无法打开与SQL Server [53]的连接。(53)(53)(SQLDRiverConnect); [08001] [Microsoft] [SQL Server的ODBC驱动程序18]登录超时到期(0); SQL Server无法找到SQL名称,如果SQL Server配置为远程连接,请参见SQL Server Books。

。 SQL Server的驱动程序17该错误是:

找不到数据源名称,未指定默认驱动程序

,当我使用“ SQL Server”时,代码返回上面的错误。我通过运行选择@@ @ @servername获得了数据库名称,并且显然使用了我最初为容器创建的密码。

我对数据库和表的疑问是有效的,因为我在终端中看到结果:

”终端显示数据库

Using Docker, I'm creating a SQL Server 2019 image & container and I currently cannot execute my Python script which accesses the database and prints out the results within specific table. the SQL Server is running in a docker container. I'm accessing the script within the terminal which is just a python file located inside my local directory.

Here's my code:

import pyodbc
connector = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};' 'Server=cbbcb967dacb;' 'Database=testdb;' 'UID=sa;' 'PWD=Password' 'Trusted_Connection=yes;')
connector.execute()

cursor = connector.cursor()
cursor.execute('SELECT * FROM inventory')

for i in cursor:
    print(i)

The error I get is

pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 18 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.

I downloaded the ODBC driver 18 prior and when I change driver to 'ODBC Driver 17 for SQL Server' the error is:

Data source name not found and no default driver specified

and when I use 'SQL Server', the code returns the error above. I got the database name by running select @@servername and obviously used the password I initially created for the container.

My queries to the database and table are valid as I see the results in terminal:

terminal showing database

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文