SQL Alchemy Check Connection

Viewed 5186

I created the below method to connect to SQL Server using SQL Alchemy and Pyodbc.

def getDBEngine(server, database):
    Params = urllib.quote_plus("DRIVER={SQL Server};SERVER="+server+";DATABASE="+database+";TRUSTED_CONNECTION=Yes")
    Engine = sa.create_engine("mssql+pyodbc:///?odbc_connect=%s" % Params)

    return Engine

I'm able to then use that engine to read and write data via methods like to_sql from pandas per the below.

def toSQL(Server, Database, Tablename):
    writeEngine= getDatabaseEngine(Server, Database)
    data.to_sql('write_Tablename',writeEngine,if_exists='append')

My question is whether there is a simple way to check the connection/ status of the engine before actually using it to read or write data. What's the easiest way?

1 Answers
Related