安装Mac的ODBC驱动程序pyodbc

时间:2018-09-21 18:57:11

标签: python sql-server macos odbc

与我的Mac一起尝试连接到SQL Server数据库。在Microsoft's instructions之后。但是,当我输入:

brew install --no-sandbox msodbcsql@13.1.9.2 mssql-tools@14.0.6.0

我得到了错误:

odbcinst: SQLInstallDriverEx failed with Invalid install path.

如果我安装了ODBC驱动程序(在Microsoft的说明网页顶部)并手动加载到python脚本中,那可能吗?这是我的python代码:

import pyodbc
import pandas as pd

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                       "Server=server_name;"
                       "Database=Sandbox;"
                       "uid=username;pwd=password")
df = pd.read_sql_query('select * from table', cnxn)

在这段代码中,如何将压缩的下载驱动程序替换为connect语句,从而绕过brew?

1 个答案:

答案 0 :(得分:0)

  

在这段代码中,如何将压缩的下载驱动程序替换为connect语句,从而绕过brew?

答案是否定的,您不能做您要问的事情-您在跨目的交谈。 brew(Homebrew)是程序包管理器命令,在这种情况下,此命令需要在Mac上安装驱动程序,以便Python代码可以与数据库对话。简而言之,没有安装驱动程序就无法运行Python代码,因此无法绕过它。

用于安装驱动程序的Homebrew水龙头(https://github.com/Microsoft/homebrew-mssql-release)中发生了更新,其中包括一些与您的驱动程序类似的issues,现已关闭/解决。因此,请尝试以下操作:

# This updates the tap and Homebrew to the latest versions
brew update

# Install the drivers; note options like `--no-sandbox` are no longer in Homebrew
brew install msodbcsql@13.1.9.2 mssql-tools@14.0.6.0

这可以在我的带有Homebrew 2.0.1的macOS 10.14环境中使用。检查/usr/local/etc/odbcinst.ini处的文件,您应该看到已定义的对[ODBC Driver 13 for SQL Server]的引用。您需要更改Python代码以反映此新驱动程序的名称,如下所示:

cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"