python-3.x - Mac OS X 上的 ODBC Sql Server 17 驱动程序

标签 python-3.x macos pyodbc unixodbc

为了执行这个 python MAC 上的代码 [适用于 Ubuntu 18],

import pyodbc as odbc

sql_conn_irs = odbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=xxx.xxx.xxx.xxx;DATABASE=irs;UID=xxxxx;PWD=xxxxxxxx;')

我安装了 SQL Server Driver像这样:
mm2:~ ivan$ HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
==> Installing msodbcsql17 from microsoft/mssql-release
==> Installing dependencies for microsoft/mssql-release/msodbcsql17: unixodbc
==> Installing microsoft/mssql-release/msodbcsql17 dependency: unixodbc
==> Downloading https://homebrew.bintray.com/bottles/unixodbc-2.3.7.mojave.bottle.1.tar.gz
==> Downloading from https://akamai.bintray.com/d9/d9f30688c0639d73b9e3a4b4b94d3679b2762e2cf0bff6b2ad64fcd175cc30ca?__gda__=exp=1581885422~hmac=2d3
######################################################################## 100.0%
==> Pouring unixodbc-2.3.7.mojave.bottle.1.tar.gz
🍺  /usr/local/Cellar/unixodbc/2.3.7: 46 files, 1.8MB
==> Installing microsoft/mssql-release/msodbcsql17
==> Downloading https://download.microsoft.com/download/1/9/A/19AF548A-6DD3-4B48-88DC-724E9ABCEB9A/msodbcsql-17.5.1.1.tar.gz
######################################################################## 100.0%
==> odbcinst -u -d -n "ODBC Driver 17 for SQL Server"
==> odbcinst -i -d -f ./odbcinst.ini
==> Caveats
If you installed this formula with the registration option (default), you'll
need to manually remove [ODBC Driver 17 for SQL Server] section from
odbcinst.ini after the formula is uninstalled. This can be done by executing
the following command:
    odbcinst -u -d -n "ODBC Driver 17 for SQL Server"
==> Summary
🍺  /usr/local/Cellar/msodbcsql17/17.5.1.1: 9 files, 2MB, built in 37 seconds
==> Installing mssql-tools from microsoft/mssql-release
==> Downloading https://download.microsoft.com/download/F/D/1/FD16AA69-F27D-440E-A15A-6C521A1972E6/mssql-tools-17.5.1.2.tar.gz
######################################################################## 100.0%
🍺  /usr/local/Cellar/mssql-tools/17.5.1.2: 11 files, 869KB, built in 7 seconds
==> Caveats
==> msodbcsql17
If you installed this formula with the registration option (default), you'll
need to manually remove [ODBC Driver 17 for SQL Server] section from
odbcinst.ini after the formula is uninstalled. This can be done by executing
the following command:
    odbcinst -u -d -n "ODBC Driver 17 for SQL Server"

然后我执行它以查看它返回的内容:
mm2:~ ivan$ odbcinst -j
unixODBC 2.3.4
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/ivan/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

然而,当我找到那个文件时,
mm2:~ ivan$ more /etc/odbcinst.ini
/etc/odbcinst.ini: No such file or directory
mm2:~ ivan$

当我运行 python代码,我得到:
---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-8-d2b1c4f70099> in <module>()

---> 11 sql_conn_irs = odbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=xxx.xxx.xxx.xxx;DATABASE=irs;UID=xxxx;PWD=xxxxx;')
     12 query = "SELECT * FROM Table_Curve order by Date asc"
     13 dfirs = pd.read_sql(query, sql_conn_irs)

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

在 Mac OS X 上安装 SQL Server 驱动程序是否有额外的魔力?

最佳答案

我在 mac OS catalina 上通过卸载 e 安装修复了它

这是同样的错误,然后我做了什么,首先我按照以下说明安装 Brew:

https://docs.microsoft.com/pt-br/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew install msodbcsql17 mssql-tools


不管终端消息如何,一些 itens 已被弃用,我使用了来自终端和微软网站的说明,我只使用了上面的几行。

安装1,没有用,然后我卸载了包并重新安装

brew uninstall msodbcsql17 mssql-tools 

brew install msodbcsql17 mssql-tools 


在第一次安装终端没有显示所有许可证消息警报,第二次安装终端显示两个许可证警报:

erik@ERIKs-MacBook-Pro-3 ~ % brew install msodbcsql17 mssql-tools
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
broot                                    healpix

==> Installing msodbcsql17 from microsoft/mssql-release
==> Downloading https://download.microsoft.com/download/1/9/A/19AF548A-6DD3-4B48
Already downloaded: /Users/erik/Library/Caches/Homebrew/downloads/b8402326ee28ebb9b25b5c98a4fcfe0ecad17cd385a8ccb3ef5bedb0e097c2e0--msodbcsql-17.5.2.1.tar.gz
The license terms for this product can be downloaded from
https://aka.ms/odbc17eula and found in
/usr/local/share/doc/msodbcsql17/LICENSE.txt . By entering 'YES',
you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)
YES
==> odbcinst -u -d -n "ODBC Driver 17 for SQL Server"
==> odbcinst -i -d -f ./odbcinst.ini
==> Caveats
If you installed this formula with the registration option (default), you'll
need to manually remove [ODBC Driver 17 for SQL Server] section from
odbcinst.ini after the formula is uninstalled. This can be done by executing
the following command:
    odbcinst -u -d -n "ODBC Driver 17 for SQL Server"
==> Summary
🍺  /usr/local/Cellar/msodbcsql17/17.5.2.1: 9 files, 2MB, built in 21 seconds
==> Installing mssql-tools from microsoft/mssql-release
==> Downloading https://download.microsoft.com/download/F/D/1/FD16AA69-F27D-440E
Already downloaded: /Users/erik /Library/Caches/Homebrew/downloads/ff8148cc433f9b643b44c5bbe8adec5a50a170948e0f3ad64069efe2773670c3--mssql-tools-17.5.2.1.tar.gz
The license terms for this product can be downloaded from
http://go.microsoft.com/fwlink/?LinkId=746949 and found in
/usr/local/share/doc/mssql-tools/LICENSE.txt . By entering 'YES',
you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)
YES
🍺  /usr/local/Cellar/mssql-tools/17.5.2.1: 11 files, 869KB, built in 55 seconds
==> Caveats
==> msodbcsql17
If you installed this formula with the registration option (default), you'll
need to manually remove [ODBC Driver 17 for SQL Server] section from
odbcinst.ini after the formula is uninstalled. This can be done by executing
the following command:
    odbcinst -u -d -n "ODBC Driver 17 for SQL Server"


我没有遵循这个脚本中显示的警告,我去测试连接的步骤

erik@ERIKs-MacBook-Pro-3 ~ % sqlcmd -S 192.168.2.103 -U sa -P xxxxx -Q "SELECT @@VERSION" 
                       
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64) 
	Aug 22 2017 17:04:49 
	Copyright (C) 2017 Microsoft Corporation
	Enterprise Edition (64-bit) on Windows 10 Pro 10.0 <X64> (Build 18362: ) (Hypervisor)
                                                                                             

(1 rows affected)

关于python-3.x - Mac OS X 上的 ODBC Sql Server 17 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60271649/

相关文章:

python - 如何显示输入的数字在哪个位置?

macos - 容器 "…"必须只包含一个证书及其私钥

macos - 在屏幕中心启动 OS X 应用程序

macos - git + 山狮 + dyld : lazy symbol binding failed: Symbol not found: ___strlcpy_chk

Python:将大型 SQL Server 查询结果导出到 .txt 文件

django - 在 Windows 上为 django 构建 docker 镜像时出现 gcc 错误

python - 将每个子列表项与每个其他子列表中的每个项进行匹配(python)

python-3.x - 使用机器学习算法时无法将字符串转换为 float (python 3) (Anaconda)

Python3 pathlib 单行代码用于检查 Path 是否至少有一个特定扩展名的文件

python - "No driver name specified"将 pandas 数据框写入 SQL Server 表