简体   繁体   English

无法使用 pyodbc 连接到 Azure SQL Server

[英]Cannot connect to Azure SQL Server using pyodbc

Yet another post about an Azure SQL Server connection problem from Python I'm afraid.恐怕又是一篇关于来自 Python 的 Azure SQL Server 连接问题的帖子。 I've been through multiple threads on this site, Microsoft's site and other links via Google but I cannot get this working.我已经在这个网站、微软网站和其他谷歌链接上浏览了多个线程,但我无法让它工作。

I'm using the latest version of Anaconda (Python 3.6.6) and have ODBC driver version 17.2.0.1 installed.我正在使用最新版本的 Anaconda (Python 3.6.6) 并安装了 ODBC 驱动程序版本 17.2.0.1。

Using the following script:使用以下脚本:

import pyodbc

server = server = 'myserver.database.windows.net,1433' #also tried prepending tcp:....
database = 'db1' #not the real db name
username = 'username@myserver' #also tried username@mydomain.com
password = '123456' #obviously not my real password!
driver= '{ODBC Driver 17 for SQL Server}' #also tried v13 ODBC driver and 'SQL Server' here

cnxn = pyodbc.connect('DRIVER='+driver+';PORT=1433;SERVER='+server+';DATABASE='+database+';UID='+username)

cursor = cnxn.cursor()

I get the following error:我收到以下错误:

Error: ('IM004', "[IM004] [Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle on SQL_HANDLE_ENV failed (0) (SQLDriverConnect)")

Can anybody offer any insights please?有人可以提供任何见解吗?

I know this answer is a little late, but in case anyone else needs help.我知道这个答案有点晚了,但以防万一其他人需要帮助。

The official Azure documentation, shows parameters which are different to what is actually required: Azure 官方文档显示的参数与实际需要的参数不同:

https://docs.microsoft.com/en-us/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-2017

The solution that did work for me was to access the connection string in the database portal in my Azure dashboard.对我有用的解决方案是访问 Azure 仪表板中数据库门户中的连接字符串。

If you navigate to the database instance you're trying to connect to and select Overview:如果您导航到您尝试连接的数据库实例并选择概览: 在此处输入图片说明

Then on the right hand side you should find Connection strings:然后在右侧,您应该找到连接字符串: 在此处输入图片说明

And then you should be able to select the ODBC connection string which only requires that you replace your password:然后您应该能够选择 ODBC 连接字符串,它只需要您替换密码: 在此处输入图片说明

I suspect that the thing that catches most people out is selecting the appropriate driver.我怀疑最能吸引大多数人的是选择合适的驱动程序。 As you can see in this string and in the pyodbc documentation you have to specify which driver to use based on what your DB is running on:正如您在此字符串和 pyodbc 文档中看到的,您必须根据您的数据库运行的内容指定要使用的驱动程序:

https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows

This solution worked first time for me, provided that I had added my IP to the DB firewall.这个解决方案第一次对我有用,前提是我已经将我的 IP 添加到数据库防火墙。

all the rest same run print(pyodbc.drivers()) driver= '{SQL Server}'` try this solution.所有其余相同的运行print(pyodbc.drivers()) driver='{SQL Server}'` 试试这个解决方案。 then I can connect with the AZURE SQL database然后我可以连接到 AZURE SQL 数据库

I can't see anything wrong with the Python syntax and I'm unfamiliar with the error you're receiving, but Azure Documentation does indicate that you need to create a rule to allow connections from your computers public IP before you can interrogate any Azure hosted DB's with Python.我看不出 Python 语法有什么问题,我也不熟悉您收到的错误,但 Azure 文档确实表明您需要创建规则以允许来自计算机公共 IP 的连接,然后才能查询任何 Azure用 Python 托管数据库。

You might have already done this, but as it's not mentioned as something done in the question, it might be worth clarifying anyway.您可能已经这样做了,但是由于问题中没有提到它,因此无论如何可能值得澄清。

Azure Firewall Rule Set Up Azure 防火墙规则设置

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

相关问题 无法使用pyodbc连接到SQL Server - Cannot connect to sql server using pyodbc 无法使用来自另一个域的 pyodbc 连接到 SQL 服务器 - Cannot connect to SQL Server using pyodbc from another domain 使用pyodbc将Python连接到MS SQL Server - Connect Python to MS SQL Server using pyodbc Python pyodbc 使用 SQL Server 身份验证连接到 Sql Server - Python pyodbc connect to Sql Server using SQL Server Authentication 尝试使用 pyodbc 使用 python 连接到 sql server 时出错 - Error while trying to connect to sql server with python using pyodbc 无法使用 PyODBC 和 AWS Lambda 连接 RDS SQL 服务器 - Unable to connect with RDS SQL Server using PyODBC & AWS Lambda 无法在Windows 7上的Python中使用PYODBC连接到远程MS SQL服务器 - Cant Connect to remote MS SQL server using PYODBC on Windows 7 in Python 尝试使用 pyodbc 连接到 SQL Server 数据库时出现 OperationalError - OperationalError when trying to connect to SQL Server database using pyodbc 无法使用 windows 身份验证通过 pyodbc 连接到 SQL Server 2005 - Not able to connect to SQL Server 2005 through pyodbc using windows authentication 使用 Pyodbc 连接到 SQL 服务器,无需密码字符串和证书 - Connect to SQL server using Pyodbc with out password string and a certificate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM