pyodbc on google colab

余生长醉 提交于 2020-12-26 18:50:49

问题


I am trying to connect to my SQL server on google colab by using pyodbc. However, it tells me that I cannot find the driver.

Code to install packages (I replaced IP, port and password with x)

``` !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()
```

I get this error:

```---------------------------------------------------------------------------
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)")
``` 

Do you have any suggestion how to make it work?

Thanks!


回答1:


I had the same problem, but finally figured out that installing the ODBC package direct from microsoft could solve it. Run the following code as a single batch in 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



回答2:


Thanks both of you, this worked for me:

%%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
!sudo apt-get install unixodbc-dev
!pip install pyodbc
!pip install chart_studio

import pyodbc


来源:https://stackoverflow.com/questions/61519253/pyodbc-on-google-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!