SqlAlchemy equivalent of pyodbc connect string using FreeTDS

后端 未结 5 1572
借酒劲吻你
借酒劲吻你 2020-12-08 01:37

The following works:

import pyodbc
pyodbc.connect(\'DRIVER={FreeTDS};Server=my.db.server;Database=mydb;UID=myuser;PWD=mypwd;TDS_Version=8.0;Port=1433;\')
         


        
5条回答
  •  醉酒成梦
    2020-12-08 01:48

    The example by @Singletoned would not work for me with SQLAlchemy 0.7.2. From the SQLAlchemy docs for connecting to SQL Server:

    If you require a connection string that is outside the options presented above, use the odbc_connect keyword to pass in a urlencoded connection string. What gets passed in will be urldecoded and passed directly.

    So to make it work I used:

    import urllib
    quoted = urllib.quote_plus('DRIVER={FreeTDS};Server=my.db.server;Database=mydb;UID=myuser;PWD=mypwd;TDS_Version=8.0;Port=1433;')
    sqlalchemy.create_engine('mssql+pyodbc:///?odbc_connect={}'.format(quoted))
    

    This should apply to Sybase as well.

    NOTE: In python 3 the urllib module has been split into parts and renamed. Thus, this line in python 2.7:

    quoted = urllib.quote_plus
    

    has to be changed to this line in python3:

    quoted = urllib.parse.quote_plus
    

提交回复
热议问题