sql - 如何使用 Python3 连接到在 mac 上的 docker 内运行的 SQL Server?
问题描述
我想使用 Python 连接到在 Docker 容器中运行的 SQL Server 数据库。目前,我面临的问题
错误:('01000',“[01000] [unixODBC][Driver Manager]无法打开库'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'))
错误出在连接线本身。
更新
我尝试了以下这篇文章。有一个转移点:我在我的 Mac 中找不到 unixODBC 配置文件。但是有一个目录,/usr/local/Cellar/unixodbc/<version>/
. 注意:由于文档指出必须有一个etc
目录,所以没有。所以,我创建了一个并在其中创建了两个文件:odbcinst.ini
和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获得。完整的参考资料在这里。
如果您使用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
这可能会在您的文件中正确安装 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>'
推荐阅读
- mongoose - 如何在 GraphQLObjectType 中定义对象字段?
- php - 获取扩展类的对象变量
- angular - 我没有得到 rxjs 6 和 angular 6 的间隔、switchMap 和 map
- sql-server - 如何删除 SQL Server 中的主键列
- php - 添加 doctype html 时,输入字段 CSS 代码无法正确呈现
- javascript - 如何为具有不同 ID 的不同元素调用相同的方法?
- node.js - 从 GET 方法中将 Tweet 数据发送到 PUG 模板
- php - PDO sql插入未执行且未显示错误
- javascript - 从 Firebase 网络检索数据
- python - 在 python 上迭代时在 dict 中存储和更新值