ran print(pyodbc.drivers())
,它打印了可用驱动程序的列表...在Docker中运行一个SQL容器,我试图从Python脚本操纵
connector = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};' 'Server=cbbcb967dacb;' 'Database=testdb;' 'UID=sa;' 'PWD=Working@2022' '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 Server配置为远程连接
。
Ran print(pyodbc.drivers())
and it printed the list of available drivers... running a SQL container in docker I'm trying to manipulate the database from a python script
connector = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};' 'Server=cbbcb967dacb;' 'Database=testdb;' 'UID=sa;' 'PWD=Working@2022' '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. (53)')
But the ODBC Driver is installed
发布评论
评论(1)
例外情况建议是网络错误,
cbbcb967dacb
看起来像容器的主机名,您可能需要直接尝试其IP地址,或者如果您按照docker在docker中运行SQL Server,则根据文档。您是否可以
telnet CBBCB967DACB 1433
?根据评论中的讨论,如果您的Docker容器正在运行并根据MSSQL教程启动,则以下连接字符串应起作用:
The exception suggests a network error,
cbbcb967dacb
looks like the hostname of the container, you might want to try its IP address directly or localhost if you ran SQL Server in docker as per the documentation.Are you able to
telnet cbbcb967dacb 1433
?Based on the discussion in the comments, if your docker container is running and was started as per the MSSQL tutorial, the following connection string should work: