1

Where is the server name found in the SQL Alchemy session or engine?

I prefer not parsing the query property.

e = connManager.get_engine()

e.engine.url

mssql+pyodbc:///?odbc_connect=DRIVER={SQL Server Native Client 11.0};SERVER=myhost;DATABASE=mydatabase;TRUSTED_CONNECTION=Yes
database:''
drivername:'mssql+pyodbc'
host:None
password:None
port:None
query:{'odbc_connect': 'DRIVER={SQL Server ...CTION=Yes'}
'odbc_connect':'DRIVER={SQL Server Native Client 11.0};SERVER=myHost;DATABASE=mydatabase;TRUSTED_CONNECTION=Yes'
__len__:1
username:None
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
dirtyw0lf
  • 1,899
  • 5
  • 35
  • 63

1 Answers1

1

I don't think the server name alone is stored in the Engine object anywhere, but you could always ask pyodbc to interrogate the driver:

import pyodbc

# ...

cnxn = engine.raw_connection()
server_name = cnxn.getinfo(pyodbc.SQL_SERVER_NAME)
print(server_name)  # GORD-HP\SQLEXPRESS
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418