Sqlalchemy pyodbc无DNS的URL连接字符串似乎不起作用

时间:2015-11-14 22:13:29

标签: python sqlalchemy sybase-ase pyodbc

我正在尝试使用sqlalchemy(1.0.9)和pyodbc连接到Windows上的Sybase ASE 15数据库。如果我使用DNS网址,一切都按预期工作:

url = 'sybase+pyodbc://username:password@host:port/database?driver=Adaptive Server Enterprise'

但是,如果我避开DNS,我会收到错误消息:

bindMethod(new, self.newFile)

,我收到一个错误:

  

DBAPIError:(pyodbc.Error)('01S00','[01S00] [SAP] [ASE ODBC   驱动程序]无效的端口号(30011)(SQLDriverConnect)')

端口号是正确的,它与DNS中指定的端口相同。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

尝试使用pyODBC(和一般的ODBC)使用的分号分隔的无DSN格式的某些版本可能是值得的。这里有一些例子:

http://www.connectionstrings.com/adaptive-server-enterprise-odbc-driver/

这个问题解决了与FreeTDS类似的问题,但概念是一样的,因为连接字符串基本上是传递给低级别的ODBC连接:

SqlAlchemy equivalent of pyodbc connect string using FreeTDS

URL正在被解析为这种类型的字符串,以便通过pyodbc到SQLDriverConnect(在ODBC API中)进行最终连接,因此直接指定ASE ODBC无DSN连接字符串可能会更好。

更新:快速测试以查看为此网址生成的连接参数:

from sqlalchemy.engine.url import *
from sqlalchemy.connectors.pyodbc import *

connector =  PyODBCConnector()
url = make_url("sybase+pyodbc://username:password@host:5555/database?driver=Adaptive Server Enterprise")
print connector.create_connect_args(url)

这导致:

[['DRIVER={Adaptive Server Enterprise};Server=host,5555;Database=database;UID=username;PWD=password'], {}]

请注意,主机名和端口用逗号分隔,每个http://www.connectionstrings.com/adaptive-server-enterprise-odbc-driver/tds-based-odbc-driver-from-sybase-ocs-125/,此格式适用于Sybase 12.5的基于TDS的ODBC:

Driver={Sybase ASE ODBC Driver};NetworkAddress=myServerAddress,5000;
Db=myDataBase;Uid=myUsername;Pwd=myPassword;

但是,ASE 15格式(http://www.connectionstrings.com/adaptive-server-enterprise-odbc-driver/adaptive-server-enterprise-150/)指定server=myServerAddress;port=myPortnumberport作为以分号分隔的字符串传递的键:

Driver={Adaptive Server Enterprise};app=myAppName;server=myServerAddress;
port=myPortnumber;db=myDataBase;uid=myUsername;pwd=myPassword;

如果你"作弊"在使用host;port=5555的端口规范上,您得到:

[['DRIVER={Adaptive Server Enterprise};Server=host;port=5555;Database=database;UID=username;PWD=password'], {}]

但这只是感觉就像一个坏主意™,即使它有效。我还要注意,生成的字符串使用Database作为Sybase连接字符串引用中的密钥与Db。这可能也是一个问题。

在链接问题中使用?odbc_connect可能是控制发送到ODBC的确切连接参数的最佳选择。