问题描述
我正在尝试连接到Azure SQL数据库。但不知何故,我一直收到以下错误:
Pyodbc.OperationalError:(‘08001’,‘[08001][Microsoft][ODBC驱动程序17 对于SQL Server]客户端无法建立连接(0) (SQLDriverConnect)‘)我安装了ODBC驱动程序17。
以下是我的代码:
import pyodbc
server = 'mftaccountinghost.database.windows.net'
database = 'mft_accounting'
username = 'localhost'
password = '######'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("select * from dbo.expense_zoho")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
以下是我在Azure SQL数据库上的连接字符串
有人知道为什么会弹出这个错误吗?
谢谢。
推荐答案
确保您安装了正确的Python SQL Driver。
请参考本教程:Quickstart: Use Python to query an Azure SQL database。
以下是我的示例pyodbc
代码,它在我的窗口中运行良好。
import pyodbc
server = 'sqlserverleon.database.windows.net'
database = 'Mydatabase'
username ='ServerAdmin'
password = '****'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+
';SERVER='+server+
';PORT=1433;DATABASE='+database+
';UID='+username+
';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT * FROM TEST6")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
希望这能有所帮助。