问题
I'm trying to use pyodbc to connect to a SQL Server (MS SQL Server through FreeTDS) in a portable application; since it's supposed to be standalone, I would like to avoid having to explicitly install the driver on the system, just bringing the ODBC driver dll along the application.
This page suggests that it's possible to specify the driver dll directly in the connection string
Specify the
DRIVER=
parameter in theszConnStrIn
argument to theSQLDriverConnect
function. For example:szConnStrIn = "driver=ospath/dbodbc6.dll;dbf=c:\asademo.db"
where
ospath
is the operating system subdirectory of your Adaptive Server Anywhere installation directory.
Trying it through pyodbc+libtdsodbc.so
on Linux, it does work fine; however, trying the same on Windows (pyodbc+tdsodbc.dll
) I always get
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(my libtdsodbc.so
seems to be fine, though, since, if I install it as a "regular" driver and refer it with its name it connects fine)
Checking the documentation of SQLDriverConnect and related pages there's no mention of the DRIVER=
option used straight with the dll path.
So, isn't "straight-to-driver-dll" connection not supported on Windows? Are there any alternatives, especially with Python, to connect straight to the driver dll, bypassing the ODBC driver manager?
回答1:
It's likely impossible to troubleshoot this now, 2.5 years later, but my best guess would be a typo or permissions error on the unregistered libtdsodbc.so
, or some other error in your SQLDriverConnect()
call (which you did not provide, so cannot be analyzed).
I think you must not have read the documentation of SQLDriverConnect() carefully or completely, as it explicitly includes DRIVER in the EBNF of SQLDriverConnect() --
empty-string ::=attribute ::= attribute-keyword=attribute-value | DRIVER=[{]attribute-value[}]
-- and discusses it further in the details of the "Comments" section.
Note that this does not "bypass the ODBC driver manager," though it does make a DSN-less connection (so does not refer to the odbc.ini
Registry trees on Windows, nor the odbc.ini
file(s) on other OS).
来源:https://stackoverflow.com/questions/40263138/is-it-possible-to-specify-the-driver-dll-directly-in-the-odbc-connection-string