[英]Unable to connect to SQL Server ODBC using python
I am trying to connect to SQL Server from python to read synapse views.我正在尝试从 python 连接到 SQL 服务器以读取突触视图。 I have server name, user name and password.
我有服务器名称、用户名和密码。 I am using the below code.
我正在使用下面的代码。
import pyodbc
server = ''
database = ''
username = ''
password = ''
cnxn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER='+server+';DATABASE='+database+';ENCRYPT=yes;UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
Below is the error message I receive.以下是我收到的错误消息。
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 18 for SQL Server': file not found (0) (SQLDriverConnect)")
错误:('01000',“[01000] [unixODBC] [Driver Manager]无法打开 lib 'ODBC Driver 18 for SQL Server':找不到文件(0)(SQLDriverConnect)”
Should I use JDBC connection from Python?我应该使用来自 Python 的 JDBC 连接吗?
You can try this alternative approach 17 version which is working for me.您可以尝试适用于我的替代方法 17 版本。 try to down grade version 18 to 17.
尝试将版本 18 降级到 17。
I tried to reproduce the same in my environment and got the below results with 17:我尝试在我的环境中重现相同的内容,并使用 17 获得以下结果:
Run following command to install ODBC drivers on the Azure Databricks cluster运行以下命令在 Azure Databricks 集群上安装 ODBC 驱动程序
%sh
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get -q -y install msodbcsql17
Now, you can check Azure Databricks connected to SQL server现在,您可以检查连接到 SQL 服务器的 Azure Databricks
Code:
代码:
import pyodbc
server = '<server_name>'
database = '<Database_name>'
username = '<User_name>'
password = '<SQL_password>'
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE='+ database +';UID=' + username + ';PWD='+ password)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.