【问题标题】:SQL Alchemy Check ConnectionSQLAlchemy 检查连接
【发布时间】:2018-01-14 01:44:24
【问题描述】:

我创建了以下方法来使用 SQL Alchemy 和 Pyodbc 连接到 SQL Server。

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

然后,我可以使用该引擎通过以下方法从 pandas 中读取和写入数据,例如 to_sql。

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

我的问题是,在实际使用引擎读取或写入数据之前,是否有一种简单的方法来检查引擎的连接/状态。最简单的方法是什么?

【问题讨论】:

    标签: sql-server python-2.7 orm sqlalchemy pyodbc


    【解决方案1】:

    我在多次参与中看到的一种模式本质上是一种“有效”检查,实际上是select 1 as is_alive;。没有数据访问,所以它只检查您的连接在哪里可以接收来自您的应用程序的命令。

    【讨论】:

    • 这将产生 QThread: Destroyed while thread is still running in Qt-based program if there's no connection to the database
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多