pythonpyodbcqodbc

Parameterize pyodbc connect string using keyword arguments from my qodbc API?


Help me to understand if I can parameterize my connection string for a pyodbc connection to this qodbc interface for QuickBooks:

pyodbc.connect(r'DSN=qremote_dsn;IPAddress=192.168.0.50;Port=4500;RemoteDSN=login_dsn;OpenMode=F;OLE DB Services=-2;', autocommit=True)

I have several different DSNs, RemoteDSNs and servers which I'd like to loop over. Several SO posts (here and here) point to this code.google documentation suggesting I can use strings, keywords, or both with pyodbc's connect function.

pyodbc works with many different odbc APIs, so how do determine if any of the pyodbc keywords will map to my required qodbc keywords?

My search for the qodbc keywords in the pyodbc documentation returns no results. Must I conclude f-strings are my only option?


Solution

  • Must I conclude f-strings are my only option?

    Not at all.

    pyodbc is built to deal with any ODBC driver, so it does not identify which keywords are "legal" and which ones arent. As explained here there are a few keywords that are specified by the DBAPI spec, and a few keywords reserved for pyodbc internal use, and they "are not passed to the odbc driver" implying that other keywords are passed to the ODBC driver.

    Example: When I use this connect call ...

    cnxn = pyodbc.connect(
        driver="ODBC Driver 17 for SQL Server",
        server="192.168.0.179,49242",
        database="myDb",
        uid="sa", pwd="_whatever_",
        trusted_connection="no"
    )
    

    ... the ODBC trace shows that this is the connection string passed to the driver

    [ODBC][2404][1589493655.363466][SQLDriverConnectW.c][290]
            Entry:
                Connection = 0xf7d9c0
                Window Hdl = (nil)
                Str In = [driver=ODBC Driver 17 for SQL Server;server=192.168.0.179,49242;database=myDb;uid=sa;pwd=_whatever_;trusted_connection=no;][length = 122 (SQL_NTS)]
    

    Note that trusted_connection is specific to SQL Server.