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

标签 sql sql-server python-3.x docker

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

Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (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'))


错误出在连接线本身。

更新

我尝试了以下 this文章。有一个转移点:我在我的 Mac 中找不到 unixODBC 配置文件。但是有一个目录,/usr/local/Cellar/unixodbc/<version>/ .注意:正如文档所述,必须有 etc目录,没有。因此,我创建了一个并在其中创建了两个文件:odbcinst.iniodbc.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 获得.完整引用是 here .

如果您使用 Homebrew , 你可以:

  • 安装unixodbc
  • brew install unixodbc
    
  • 点击 Microsoft 存储库
  • brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
    
  • 更新和安装
  • brew update
    brew install msodbcsql17 mssql-tools
    

    这可能会在您的 /usr/local/etc/odbcinst.ini 中正确安装 unixodbc 驱动程序。文件(应该,但对我来说是 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>'
    

    关于sql - 如何使用 Python3 连接到在 mac 上的 docker 内运行的 SQL Server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57940182/

    相关文章:

    sql - 在 SQL 中编写函数以循环访问 UDF 中的日期范围

    sql - 列存储索引 - 偏移获取查询的性能缓慢

    sql-server - 如何设置我的部署选项来编写 Visual Studio 2010 数据库项目的增量发布脚本?

    python - 如何使用 Windows 操作系统从 .pkl 文件获取数据

    python - 我的 OpenCV remap()ing 有什么问题?

    mysql - 您可以在 (MySQL) sql 查询中使用字段注释/描述作为别名吗?

    MySQL 解决错误 1093

    sql - 对列值的更改 date_trunc 进行不同搜索

    sql-server - Linq to SQL 审计跟踪/审计日志 : should I use triggers or doddleaudit?

    python - 无法在 Pycharm 上安装 python 库