python - sqlalchemy 关闭所有连接

标签 python sqlalchemy

我正在尝试使用 sqlalchemy 删除我的数据库:

def dropDb(self, dbName):
    self.closeConnection()
    self.selectDb(None)
    self.execute("drop database %s" % dbName)

def closeConnection(self):
    self._Session.close_all()
    self._engine.dispose()
    del self._Session
    del self._engine
我用以下方法创建引擎:
sqlalchemy.create_engine(connection_string, poolclass = NullPool)
但我收到以下错误:

Detail ProgrammingError: (ProgrammingError) ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot drop database "test_db" because it is currently in use. (3702) (SQLExecDirectW)') 'drop database test_db' ()


如何强制关闭所有连接?

最佳答案

The current method使用 SQLAlchemy session api 关闭所有连接是

from sqlalchemy.orm import close_all_sessions

close_all_sessions()
session.close_all()已弃用。

关于python - sqlalchemy 关闭所有连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993416/

相关文章:

python - Flask-Admin多对多字段展示

python - 如何找到将 N 个观察值分配到 M 个组的最佳方法?

python - Selenium 蟒 : How to get css without targetting a specific class/id/tag

javascript - Python 网页抓取 : BeautifulSoup not showing all html source content

python - SQLAlchemy 一对一关系创建多行

mysql - 从表中选择 *,其中 id != long_list

Python - 如果除法有余数则抛出异常?

python - 如何在Python中读取具有可变多行数据的文件

python - sqlalchemy 动态过滤

python - 我如何结合 SQLAlchemy 和 EAV DB 模式的优势?