尝试使用SQLAlchemy以pyodbc
(freeTDS)作为驱动程序连接到SQL Server;如果直接使用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中 return self._pool.get(wait,self._timeout)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/queue.py", 第145行,在得到 提升空sqlalchemy.util.queue.Empty
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):文件"",第1行,in 文件 " /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py" ;, 第2091行,在连接中 return self._connection_cls(self,** kwargs)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py", 第90行,在 init 中 如果连接不是其他无引擎engine.raw_connection()文件" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py", 第2177行,在raw_connection中 self.pool.unique_connection,_connection)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py", 第2147行,在_wrap_pool_connect中 return fn()File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py", 第328行,在unique_connection中 return _ConnectionFairy._checkout(self)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py", 第766行,在_checkout中 fairy = _ConnectionRecord.checkout(pool)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py", 第516行,结帐时 rec = pool._do_get()File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py", 第1138行,在_do_get中 self._dec_overflow()File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", 第66行,在退出 compat.reraise(exc_type,exc_value,exc_tb)File" /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中 return self._create_connection()File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py", 第_ 333行,在_create_connection中 return _ConnectionRecord(self)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py", 第461行,在 init 中 self。 connect(first_connect_check = True)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py", 第661行,在__connect中 exec_once(self.connection,self)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/event/attr.py", 第246行,在exec_once中 self(* args,** kw)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/event/attr.py", 第256行,在__call 中 fn(* args,** kw)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", 1331行,去吧 return once_fn(* arg,** kw)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py", 第181行,在first_connect中 dialect.initialize(c)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/connectors/pyodbc.py", 第165行,初始化 super(PyODBCConnector,self).initialize(connection)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/dialects/mssql/base.py", 第1742行,初始化 super(MSDialect,self).initialize(connection)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/default.py", 第256行,初始化 self.get_isolation_level(connection.connection)File" /Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/dialects/mssql/base.py", 第1735行,在get_isolation_level中 "尝试过的观点:%s;最终错误是:%s" %(views,err))UnboundLocalError:局部变量'错误'在分配前引用
我已尝试安装和卸载sqlalchemy并在google上搜索,但找不到解决方案。有没有人有类似的问题,并对发生的事情有所了解?
操作系统信息:
ProductName: Mac OS X
ProductVersion: 10.12.6
BuildVersion: 16G29
答案 0 :(得分:0)
这是我用来解决类似症状的连接问题的连接字符串:
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)
ref http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#pass-through-exact-pyodbc-string