如何使用 Python3 连接到 mac 上 docker 内运行的 SQL Server?

2024-01-10

我想使用 Python 连接到在 Docker 容器中运行的 SQL Server 数据库。目前,我面临的问题是

错误:('01000',“[01000] [unixODBC][驱动程序管理器]无法打开 lib 'SQL Server': 找不到文件 (0) (SQLDriverConnect)”)

我已按照文档进行操作,并按照所述进行了所有操作。

我尝试使用以下连接字符串:

connection_string = 'DRIVER={SQL Server};SERVER=localhost;DATABASE=US_NATIONAL_COPY;UID=SA;PWD=<YourStrong!Passw0rd>'
conn = db.connect(connection_string)

这产生了上述结果。

应该注意的是,我的 docker 暴露在端口号 1401 上。

当我输入命令时;curl localhost:1401,我得到以下结果:Empty reply from server.

还应该指出的是,我能够执行进入 Docker 并运行它们的 SQL 查询。

我的完整代码是:

import pyodbc as db
import pandas
connection_string = 'DRIVER={SQL Server};SERVER=localhost;DATABASE=US_NATIONAL_COPY;UID=SA;PWD=<YourStrong!Passw0rd>'
conn = db.connect(connection_string)
cursor = conn.cursor()

stateQuery = 'select numeric_id, us_state_terr, abbreviation, is_state from states'

cursor.execute(stateQuery)

stateInfo = cursor.fetchall()

# declare a dictionary
stateIsState = {}

for thisState in stateInfo:
    print (thisState[2])
    stateIsState[thisState[2]] = thisState[3]
    stateIsState[thisState[1]] = thisState[3]

datFrame = pandas.read_sql(stateQuery, conn)

for statename in datFrame.us_state_terr:
    print (statename)

def is_it_a_state(stateabbrv):
    if stateabbrv in stateIsState:
        if stateIsState[stateabbrv] == "State":
            return ("yes, " + stateabbrv + " is a state")
        else:
            return ("no, " + stateabbrv + " is not a state")
    else:
        return (stateabbrv + " is not in the dictionary")

print (is_it_a_state('NY'))

print (is_it_a_state('MP'))

print (is_it_a_state('QQ'))

错误出在连接线本身。

Update

我尝试了以下方法this https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server文章。有一个转移点:我在我的 Mac 中找不到 unixODBC 配置文件。但有一个目录,/usr/local/Cellar/unixodbc/<version>/。注意:正如文档所述,必须有一个etc目录,没有。因此,我创建了一个并在其中创建了两个文件:odbcinst.ini and odbc.ini。 前一个是:

[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL on Win32
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1

后一种情况如下:

[localhost]
Description         = Test to SQLServer
Driver              = FreeTDS
Trace               = Yes
TraceFile           = /tmp/sql.log
Database            = US_NATIONAL_COPY
Servername          = localhost:1401
UserName            = SA
Password            = <YourStrong!Passw0rd>
Port                = 1401
Protocol            = 8.0
ReadOnly            = No
RowVersioning       = No
ShowSystemTables    = No
ShowOidColumn       = No
FakeOidIndex        = No

但接下来,我的命令是:isql localhost SA '<YourStrong!Passw0rd>'失败并出现错误:[ISQL]ERROR: Could not SQLConnect我应该怎么做才能以这种方式连接?


Microsoft 为 SQL Server 提供了 unixODBC 兼容驱动程序,可从Homebrew https://brew.sh。完整的参考是here https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017.

如果您正在使用Homebrew https://brew.sh, 你可以:

  1. 安装unixodbc
brew install unixodbc
  1. 进入 Microsoft 存储库
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
  1. 更新并安装
brew update
brew install msodbcsql17 mssql-tools

这可能会在您的系统中正确安装 unixodbc 驱动程序/usr/local/etc/odbcinst.ini文件(应该是这样,但对我来说是 50/50)。如果没有,您可以从 msodbcsql17 目录复制驱动程序配置(/usr/local/Cellar/msodbcsql17/17.4.1.1/odbcinst.ini)进入你的odbcinst.ini

最终,你需要在你的/usr/local/etc/odbcinst.ini文件看起来像:

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.17.dylib

你的 python 连接字符串看起来像

connection_string = 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;DATABASE=US_NATIONAL_COPY;UID=SA;PWD=<YourStrong!Passw0rd>'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Python3 连接到 mac 上 docker 内运行的 SQL Server? 的相关文章

随机推荐