简体   繁体   English

使用pyodbc连接到Sql Server时出错

[英]Error while connecting to the Sql Server with pyodbc

I'm trying to fetch data from SQL database with pyodbc with the code given below.The connection works rarely, most of the time it gives the error, 我正在尝试使用下面提供的代码从pyodbc的SQL数据库中获取数据。该连接很少起作用,大多数情况下会出现错误,

OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC SQL Server Driver]Login timeout expired (0) (SQLDriverConnect)') OperationalError :(“ HYT00”,“ [HYT00] [Microsoft] [ODBC SQL Server驱动程序]登录超时已过期(0)(SQLDriverConnect)”)

import numpy as np
import pyodbc as odbc

conn_string = ('DRIVER={SQL Server};SERVER=test;DATABASE=DEV;UID=me;PWD=whatever;')
cnxn = odbc.connect(conn_string)
cursor = cnxn.cursor()
cursor.execute("Select * from PurchaseOrders")
rows = cursor.fetchall()
ID = [i[1] for i in rows]
ID_array = np.fromiter(ID, dtype= np.int32)

I have tried setting the timeout to zero and DRIVER={ODBC Driver 11 for SQL Server} as I'm using SQL Server 2014. None of these works. 我在尝试使用SQL Server 2014时尝试将超时设置为零,并且将DRIVER = {SQL Server的ODBC驱动程序11}设置为0。这些都不起作用。

There was a problem with DNS. DNS出现问题。 I used the IP address of the server instead, works fine now. 我改用服务器的IP地址,现在可以正常工作。

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

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