pyodbc icon indicating copy to clipboard operation
pyodbc copied to clipboard

Feature: accept sqlalchemy style connection URI

Open epa opened this issue 11 months ago • 0 comments
trafficstars

I understand that pyodbc uses a connection string looking like

DRIVER={ODBC Driver 18 for SQL Server};SERVER=myserver;DATABASE=mydb;UID=scott;PWD=tiger

But sqlalchemy has popularized its own URI scheme for database connections, something like

mssql+pyodbc://scott:tiger@myserver/mydb?driver=ODBC+Driver+18+for+SQL+Server

Of course, sqlalchemy is here acting as a layer on top of pyodbc. Sometimes I'd like to write using the pure pyodbc interface, but I have only a connection string in sqlalchemy format. Purely as a convenience, it would be great if pyodbc accepted the alternative format. Something like

cnxn = pyodbc.connect('mssql+pyodbc://scott:tiger@myserver/mydb?driver=ODBC+Driver+18+for+SQL+Server')
    # works
cnxn = pyodbc.connect('postgresql+psycopg2://scott:tiger@myserver/mydb')
    # Error: this looks like a URI but the URI scheme postgresql+psycopg2 is not handled.  Only URI schemes ending in +pyodbc are handled.

I understand that you don't want to get bogged down in handling all the weird options that sqlalchemy might accept in its connection string, nor in elaborately reproducing its behaviour. So this is kind of a best effort basis for the most common, vanilla URIs that use pyodbc.

epa avatar Dec 10 '24 17:12 epa