sqlalchemy 无法连接到 ms sql server
Posted
技术标签:
【中文标题】sqlalchemy 无法连接到 ms sql server【英文标题】:sqlalchemy fails to connect to ms sql server 【发布时间】:2018-03-01 23:28:54 【问题描述】:尝试使用带有pyodbc
(freeTDS) 作为驱动程序的SQLAlchemy 连接到SQL 服务器;如果我直接使用pyodbc
,则连接成功:
>>> import pyodbc
>>> conn = pyodbc.connect('DSN=serverdsn;UID=user;PWD=password')
>>> crsr = conn.cursor()
>>> rows = crsr.execute("select @@VERSION").fetchall()
>>> print(rows)
[('Microsoft Azure SQL Data Warehouse - 10.0.9248.28 Sep 12 2017 01:08:55 Copyright (c) Microsoft Corporation', )]
>>> crsr.close()
>>> conn.close()
但是当我使用 SQLAlchemy 时,它会因一个神秘的错误而失败:
>>> from sqlalchemy import create_engine
>>> e = create_engine("mssql+pyodbc://user:password@serverdsn")
>>> with e.connect() as con:
... rs = con.execute('select * from users')
... for row in rs:
... print(row)
...
这是完整的堆栈跟踪:
Traceback(最近一次调用最后一次):文件 “/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 1122 行,在 _do_get 返回self._pool.get(等待,self._timeout)文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/queue.py”, 第 145 行,在获取 raise Empty sqlalchemy.util.queue.Empty
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):文件“”,第 1 行,in 文件 “/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py”, 第 2091 行,连接中 返回 self._connection_cls(self, **kwargs) 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py”, 第 90 行,在 init 中 如果连接不是没有其他引擎.raw_connection()文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py”, 第 2177 行,在 raw_connection 中 self.pool.unique_connection,_connection)文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py”, 第 2147 行,在 _wrap_pool_connect 返回 fn() 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 328 行,在 unique_connection 中 返回 _ConnectionFairy._checkout(self) 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 766 行,在 _checkout 中 仙女=_ConnectionRecord.checkout(池)文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 516 行,结帐时 rec = pool._do_get() 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 1138 行,在 _do_get self._dec_overflow() 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py”, 第 66 行,在 退出 compat.reraise(exc_type,exc_value,exc_tb)文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/compat.py”, 第 187 行,在加注中 提高价值文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 1135 行,在 _do_get 返回 self._create_connection() 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 333 行,在 _create_connection 中 返回_ConnectionRecord(self)文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 461 行,在 init 中 self.connect(first_connect_check=True) 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py”, 第 661 行,在 __connect exec_once(self.connection,self)文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/event/attr.py”, 第 246 行,在 exec_once self(*args, **kw) 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/event/attr.py”, 第 256 行,在 __call 中 fn(*args, **kw) 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py”, 第 1331 行,进入 返回 once_fn(*arg, **kw) 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py”, 第 181 行,在 first_connect dialect.initialize(c) 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/connectors/pyodbc.py”, 第 165 行,初始化 超级(PyODBCConnector,self).initialize(连接)文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/dialects/mssql/base.py”, 第 1742 行,初始化 超级(MSDialect,self).initialize(连接)文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/default.py”, 第 256 行,初始化 self.get_isolation_level(connection.connection) 文件“/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/dialects/mssql/base.py”, 第 1735 行,在 get_isolation_level "尝试过的视图:%s;最终错误是:%s" % (views, err)) UnboundLocalError: 在赋值之前引用了局部变量 'err'
我已经尝试安装和卸载 sqlalchemy 并在谷歌上搜索,但没有找到解决方案。有没有人有类似的问题并且知道发生了什么?
操作系统信息:
ProductName: Mac OS X
ProductVersion: 10.12.6
BuildVersion: 16G29
【问题讨论】:
【参考方案1】:这是我用来解决类似症状的连接问题的连接字符串:
import urllib
from sqlalchemy import create_engine
# utilize existing odbc connection to create engine
params = urllib.quote_plus("DRIVER=; SERVER=server; Database=database; UID=user; PWD=pw")
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
参考http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#pass-through-exact-pyodbc-string
【讨论】:
以上是关于sqlalchemy 无法连接到 ms sql server的主要内容,如果未能解决你的问题,请参考以下文章
无法在远程系统上使用 Windows 身份验证连接到 ms sql server
使用 sqlalchemy 和 pyodbc 连接到 SQL Server 2012