3

This works:

db = pyodbc.connect('driver={SQL Server Native Client 11.0}; server=172.30.0.194; database=db;uid=someuser; pwd=fancy@password')

This doesn't

cn_string = "mssql+pyodbc://someuser:"fancy&password"@172.30.0.194/db?driver=SQL+Server+Native+Client+11.0"
return create_engine(cn_string)

This doesn't either:

driver = "SQL Server Native Client 11.0"
server = "192.30.0.194"
database = "EPM_Dashboard"
uid = "someuser"
pwd = "fancy@password"
params = f'DRIVER={{{driver}}};SERVER={server};DATABASE={database};UID={uid};PWD={{{pwd}}};'

connection_string = 'mssql+pyodbc:///?odbc_connect=%s' % urllib.parse.quote_plus(params)

return create_engine(connection_string)

I get something like:

Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (53)

which would be more believable if the pyodbc item failed.

Here's another failure:

return create_engine(urllib.parse.quote_plus('driver={SQL Server Native Client 11.0}; server=172.30.0.194; database=EPM_Dashboard;uid=someuser; pwd=fancy@password'))
    

I'm sure there's a tricky character somewhere I'm missing.

Here are some resources

https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-databases

Special character in SQL password

SqlAlchemy equivalent of pyodbc connect string using FreeTDS

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • That's not an authentication error, it's a timeout. Check the firewall is open, SQL Server is enabled for remote connection and TCP listening is enabled – Charlieface Feb 25 '21 at 15:52
  • The reason why I think all of that is fine is because on the same computer the pyodbc works. It's just the sqlalchemy create_engine that isn't working – Henrietta Martingale Feb 25 '21 at 16:00
  • What does `urllib.parse.quote_plus` do, is that the correct method for creating a connection string for sqlalchemy? (No idea, never used it myself) – Charlieface Feb 25 '21 at 16:03
  • I imagine it puts in html entities or something. This is what the first link used. I figured its the same encoding. Looks like sa may have their own different encoder? All these languages, This is why we need a monopoly in software – Henrietta Martingale Feb 25 '21 at 19:30

1 Answers1

9

If you need to construct a connection URI that may have "funny characters" in it then you can use engine.URL.create() to build it for you:

import sqlalchemy as sa

connection_uri = sa.engine.URL.create(
    "mssql+pyodbc",
    username="someuser",
    password="fancy@password",
    host="192.30.0.194",
    database="EPM_Dashboard",
    query={"driver": "SQL Server Native Client 11.0"},
)
print(connection_uri)
# mssql+pyodbc://someuser:fancy%40password@192.30.0.194/EPM_Dashboard?driver=SQL+Server+Native+Client+11.0
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418