google colab上的pyodbc

问题描述 投票:1回答:1

我正在尝试使用pyodbc连接到Google colab上的SQL服务器。但是,它告诉我找不到驱动程序。

安装软件包的代码(我用x替换了IP,端口和密码)

``` !sudo apt-get install unixodbc-dev
!pip install pyodbc
!pip install chart_studio ```

``` import pyodbc
conn = pyodbc.connect(DRIVER = '{ODBC Driver 17 for SQL Server}',
                      SERVER = 'xxx.xxx.x.xx, xxxx',
                      DATABASE = 'Database',
                      UID = 'sa',
                      PWD = 'xxxxxx')

cursor = conn.cursor()
```

我收到此错误:

```---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-12-3e54dc10e278> in <module>()
      3                       DATABASE = 'Database_PIL',
      4                       UID = 'sa',
----> 5                       PWD = 'mbdxwko2')
      6 
      7 cursor = conn.cursor()

Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
``` 

您对如何使其工作有任何建议吗?

谢谢!

python sql-server google-colaboratory pyodbc
1个回答
0
投票

我也遇到了同样的问题,但最终发现直接从Microsoft安装ODBC软件包可以解决该问题。在colab中作为一个批处理运行以下代码。

%%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
© www.soinside.com 2019 - 2024. All rights reserved.